ASP.NET MVC2 : How to render a view if has multiple different models

标签 asp.net asp.net-mvc asp.net-mvc-2

我知道 View 模型可用于呈现 View ,但如果页面需要不同的模型,我如何将它们传递给 View ?我该如何使用它们?

最佳答案

如果您需要传递多个模型,则创建一个包罗万象的模型,将较小的模型作为属性挂起。

例如,假设您要显示一个页面来管理您应用的用户组。您可能需要传递 IEnumerable<UserDisplayModel>还有一个IEnumerable<GroupDisplayModel>到 View 。像这样创建一个新的显示模型:

class GroupManagementDisplayModel
{
    public IEnumerable<UserDisplayModel> Users { get; set; }
    public IEnumerable<GroupDisplayModel> Groups { get; set; }
}

改为将此模型的实例传递给您的 View 。

关于ASP.NET MVC2 : How to render a view if has multiple different models,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4419889/

相关文章:

asp.net-mvc - DefaultModelBinder 不绑定(bind)嵌套模型

asp.net - IIS 7 中的 session 超时

javascript - 将文件存档到客户计算机上

jquery - Request.IsAjaxRequest 在 MVC3 中永远不会返回 true

c# - 我如何覆盖 :before propertie-value in/from my Html file

jquery - 是否有 JQuery 时间插件可用于在 ASP.NET MVC 页面上选择时间?

c# - ASP.NET 即使在调试中也不调用代码

c# - 重写 System.Web.Security.Roles 函数?

c# - 路由 : The current request for action [. ..] 在以下操作方法之间不明确

asp.net-mvc-2 - ASP.NET MVC 2 并列为隐藏值?