Introduction
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:
- public class HomeController : Controller
- {
- ....
-
- public async Task<actionresult> LoadPopupForOrganization(int pOrganizationId)
- {
- dynamic mymodel = new ExpandoObject();
-
-
- mymodel.dashboard = await Task.Run(() => bal.LoadDashboard(pOrganizationId, DateTime.Now));;
-
-
- mymodel.organization = new Tuple<list<DepartmentsModel>, List<InventoryModel>>(new List<DepartmentsModel>(), new List<InventoryModel>());
-
- return View("Organization", mymodel);
- }
- ...
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:
- <div class="roster-detail-box" style="border:1px solid #30B2D0">
- @{Html.RenderPartial("_OrganizationDetails", (Tuple<list<DepartmentsModel>, List<InventoryModel>>)Model.organization);}
- ...
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:
- @Model Tuple<List<DepartmentsModel>, List<InventoryModel>>
- @if (Model.Item1 != null && Model.Item1.Count > 0)
- {
- int i = 0;
- @Html.Raw("<div class=\"col-lg-4 col-md-4 col-sm-4\">")
- @Html.Raw("<div class=\"organization-box-section\">")
- DateTime renderedDate = new DateTime();
- foreach (var item in Model.Item1)
- {
- i++;
- if ((i > 0 && i < Model.Item1.Count) && (Model.Item1[i - 1].OrgtanizationName == Model.Item1[i].OrganizationName))
- {
- }
- }
- }
- ...
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.