What's the best way to organize multiple ASP.NET MVC Model Viewers in a single view?
My application has a main panel made up of 8 different partial views; each supported by their own view model and in my controller I just call
public ActionResult mainDashboard(){
return View()
}
to return the panel. My question is, has it been recommended to create a dashboard view model that also contains links to the partial view view models? What is considered the recommended best practice in this situation?
source to share
Ohkk is also a good idea to use html.Action
instead ofhtml.partial
It will look something 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 will need to create a ViewModel for the Dashboard page in a 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 will populate each list:
myModel.Users = db.MemberUsers.ToList();
... Then you will need to update the View to use this new ViewModel
@model DashboardViewModel
Finally, from within the view, you will need to pass data to each particle:
@Html.Partial("DashboardAlerts", Model.Alerts)
@Html.Partial("DashboardClubs", Model.Clubs)
source to share