Reputation: 113
My app has a main dashboard which is comprised of 8 different partial views; each backed by their own view model and in my controller I'm just calling
public ActionResult mainDashboard(){
return View()
}
to return the dashboard. My question is would it be recommended to create a dashboard view model that also contains references to the view models of the partial views? What's considered a recommended best practice in this situation?
Upvotes: 4
Views: 332
Reputation: 5778
Ohkk here is a good idea as well to use html.Action
instead of html.partial
This would look more like this:
public ActionResult Dashboard()
{
return View();
}
public PartialViewResult Clubs()
{
....
return PartialView(db.Clubs.ToList());//this assumes the partial view is named Clubs.cshtml, otherwise you'll need to use the overload where you pass the view name
}
public PartialViewResult Alerts()
{
....
return PartialView(db.Alerts.ToList());
}
Dashboard.cshtml
<div class="dashboard_alerts">
@Html.Action("Alerts")
<div class="dashboard_pending_clubs">
@Html.Action("Clubs")
</div>
<div class="dashboard_verified_members">
@Html.Action("Members")
</div>
OR
You would need to create a ViewModel specific for the Dashboard page precisely this would be more efficient way
public class DashboardViewModel
{
public IEnumerable<GMC.Models.Clubs> Clubs { get; set; }
public IEnumerable<GMC.Models.MemberUsers> Users { get; set; }
public IEnumerable<GMC.Models.Alerts> Alerts { get; set; }
}
Then, in the Dashboard action method, you would populate each list:
myModel.Users = db.MemberUsers.ToList();
... You would then need to update the view to take in this new ViewModel
@model DashboardViewModel
Finally, from within the view, you would need to pass in the data to each partial:
@Html.Partial("DashboardAlerts", Model.Alerts)
@Html.Partial("DashboardClubs", Model.Clubs)
Upvotes: 4