我一直在努力解决这个问题无济于事。使用MVC4我希望通过部分视图在单个视图上显示3个模型。单视图由3个部分视图(3个数字列表表)组成。我可以在视图中显示一个模型,但无法弄清楚如何显示其他模型。
以下是我对View Controller的看法:
[AllowAnonymous]
public ActionResult Dashboard()
{
//Database.SetInitializer<AlertsContext>(null);
Database.SetInitializer<MemberUsersContext>(null);
//Database.SetInitializer<ClubsContext>(null);
//AlertsContext db = new AlertsContext();
MemberUsersContext db = new MemberUsersContext();
//ClubsContext db = new ClubsContext();
//db.MemberUsers.ToList()
return View(db.MemberUsers.ToList());
}
这就是我作为View本身的模型(编译我删除不在@model IENumerable语句下的两个:
@model IEnumerable<GMC.Models.MemberUsers>
<div class="dashboard_alerts">
@Html.Partial("DashboardAlerts")
</div>
<div class="dashboard_pending_clubs">
@Html.Partial("DashboardClubs")
</div>
<div class="dashboard_verified_members">
@Html.Partial("DashboardMembers")
</div>
在每个部分视图中,标题如下:
DashboardClubs
@model IEnumerable<GMC.Models.Clubs>
DashboardMembers
@model IEnumerable<GMC.Models.MemberUsers>
DashboardAlerts
@model IEnumerable<GMC.Models.Alerts>
现在我的问题是如何将三个数据库上下文传递到仪表板?我很困惑,正在挣扎。
答案 0 :(得分:2)
您需要创建特定于仪表板页面的ViewModel。
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; }
}
然后,在Dashboard操作方法中,您将填充每个列表:
myModel.Users = db.MemberUsers.ToList();
...
然后,您需要更新视图以接受这个新的ViewModel
@model DashboardViewModel
最后,在视图中,您需要将数据传递给每个部分:
@Html.Partial("DashboardAlerts", Model.Alerts)
@Html.Partial("DashboardClubs", Model.Clubs)
答案 1 :(得分:2)
你有很多方法可以做到这一点并使其成为通用的,但它们大多数都有一个ViewModel。您专门为支持仪表板视图而创建的类:
public class DashboardViewModel {
public IEnumerable<GMC.Models.Clubs> Clubs {get;set;}
public IEnumerable<GMC.Models.MemberUsers> MemberUsers {get;set;}
public IEnumerable<GMC.Models.Alerts> Alerts {get;set;}
}
如果这些只是数据库中的不同表,则应通过相同的上下文访问它们,因为上下文管理您的连接和其他资源。但是,如果没有看到该代码,我就不会继续这样做。
[AllowAnonymous]
public ActionResult Dashboard()
{
//populate data into our view model(vm)
var vm = new DashboardViewModel{
Clubs = db.Clubs.ToList(),
MemberUsers = db.MemberUsers.ToList(),
Alerts = db.Alerts.ToList()
};
return View(vm);
}
仪表板视图:
@model DashboardViewModel
<div class="dashboard_alerts">
@Html.Partial("DashboardAlerts", Model.Alerts)
</div>
<div class="dashboard_pending_clubs">
@Html.Partial("DashboardClubs", Model.Clubs)
</div>
<div class="dashboard_verified_members">
@Html.Partial("DashboardMembers", Model.MemberUsers)
</div>
就个人而言,我不使用Html.Partial而是使用Action / RenderAction,因为这些将允许您实际调用另一个返回PartialViewResult的动作,因此每个动作都负责检索自己的数据。我并不总是这样做,但如果它合适,那么动作/局部视图就更可重复使用。
这看起来更像是这样:
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>
<div class="dashboard_pending_clubs">
@Html.Action("Clubs")
</div>
<div class="dashboard_verified_members">
@Html.Action("Members")
</div>