Partial View Usage With Dynamic Expando Object and Tuple

Introduction

code

We all know that an Expando object represents an object whose members can be dynamically added and removed at run time. If you want to pass an expando member to a partial view then yes you can do that.

Background

I was developing a dashboard for an organization to show details, in which I used an expando object to extend my dynamic model. I expanded my expando object to 10 models and passed some models to a partial view that will be rendered dynamically on the master page. A bulk return of data for an organization was causing the speed issue. Then I decided to use partial views and each partial will be added at run-time when the user clicks on its related menu item.

Using the code

First declare your Exapndo object to declare all your dynamic models in the controller.

Here you can review my controller code in which I have added an expando object named "mymodel" and extending it with mymodel.organization that will be passed to a parent view and then to a partial view:

  1. public class HomeController : Controller  
  2. {  
  3.    ....  
  4.    //Load dashboard popup for an organization  
  5.    public async Task<actionresult> LoadPopupForOrganization(int pOrganizationId)  
  6.    {  
  7.       dynamic mymodel = new ExpandoObject();  
  8.   
  9.       //other dynamic model added to expando  
  10.       mymodel.dashboard = await Task.Run(() => bal.LoadDashboard(pOrganizationId, DateTime.Now));;  
  11.   
  12.       //model which will be passed to partial view, it is using tuple but you can change it as  
  13.             mymodel.organization = new Tuple<list<DepartmentsModel>, List<InventoryModel>>(new List<DepartmentsModel>(), new List<InventoryModel>());  
  14.   
  15.    return View("Organization", mymodel);  
  16. }  
  17. ... 

As above, this dynamic expando object "mymodel" will be passed to the "Organization.cshtml" main view, in which I have called/rendered a partial view "_OrganizationDetails.cshtml".

From the following you can review the parent view "/Views/Organization.cshtml" in which the partial view will be rendered after recieving the mymodel.organization dynamic model:

  1. <div class="roster-detail-box" style="border:1px solid #30B2D0">  
  2. @{Html.RenderPartial("_OrganizationDetails", (Tuple<list<DepartmentsModel>, List<InventoryModel>>)Model.organization);}  
  3. ... 

From the following you can my review partial view "/Views/_OrganizationDetails.cshtml" since it is consuming mymodel.organization data with Razor to render it on the page:

  1. @Model Tuple<List<DepartmentsModel>, List<InventoryModel>>  
  2. @if (Model.Item1 != null && Model.Item1.Count > 0)  
  3. {  
  4.    int i = 0;  
  5.    @Html.Raw("<div class=\"col-lg-4 col-md-4 col-sm-4\">")  
  6.    @Html.Raw("<div class=\"organization-box-section\">")  
  7.    DateTime renderedDate = new DateTime();  
  8.    foreach (var item in Model.Item1)  
  9.    {  
  10.       i++;  
  11.       if ((i > 0 && i < Model.Item1.Count) && (Model.Item1[i - 1].OrgtanizationName == Model.Item1[i].OrganizationName))  
  12.       { 
  13.       }
  14.    }
  15. }
  16. ... 

Points of Interest

When I was worried about using partial views with dynamics in C# and while I was having dynamic expando objects in my code and I have no idea whether it will work. Then I decided to try it and I passed a dynamic model to the partial view and it worked fine. In other words MVC is very flexible and any developer can handle it easily. No need to worry, just think and implement.

The tuple can also be used when passing a model to a partial view. It is easy to access multiple types of data in a single data set. As in my example, I passed two list objects in one single tuple data set.

Up Next
    Ebook Download
    View all
    Learn
    View all