ASP.NET MVC2:如何渲染视图如果有多个不同型号 [英] ASP.NET MVC2: How to render a view if has multiple different models

查看:167
本文介绍了ASP.NET MVC2:如何渲染视图如果有多个不同型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,视图模型可以用于渲染视图,但如果一个页面需要不同的模型,我怎么能他们传递给视图?而且我怎么使用它们?

I know that view model can be used for rendering a view, but if a page needs different models, how can I pass them to the view? And how do I use them?

推荐答案

如果您需要传递多个模型然后创建具有较小的型号挂为属性包罗万象的模式。

If you need to pass multiple models then create an all-encompassing model that has the smaller models hanging off as properties.

例如,假设你要管理用户组为您的应用程序显示一个页面。你可能会需要传递一个的IEnumerable< UserDisplayModel> ,并在的IEnumerable< GroupDisplayModel> 到视图。创建的新的的显示模式是这样的:

For instance, let's say you are going to display a page for managing groups of users for your app. You would probably need to pass an IEnumerable<UserDisplayModel> and also an IEnumerable<GroupDisplayModel> to the view. Create a new display model like this:

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

通过这个模型的实例你的看法吧。

Pass instances of this model to your view instead.

这篇关于ASP.NET MVC2:如何渲染视图如果有多个不同型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆