在单个视图中的多个模型(C#MVC3) [英] Multiple Models in a Single View (C# MVC3)

查看:105
本文介绍了在单个视图中的多个模型(C#MVC3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#和MVC3。

I'm using C# and MVC3.

我有一个网页,例如学生列表,显示学生名单,这是数据库驱动。在我的菜单数据库驱动的同时,让我也有将它发送到视图。

I have a page, for example a Student list, that displays the list of students, which is database driven. At the same time my menu is database driven, so I also have to send it to the view.

我怎样才能两种型号发送到单个视图?

How can I send both models to a single view?

推荐答案

您应该始终为您的意见单独的ViewModels。应该有从您浏览一个抽象的领域模型。在演示/教程,他们表现出来的所有pretty容易通过简单的强类型的意见,领域模型,但是这不是一个好的策略。的观点不应该依赖于业务对象。

You should always create separate ViewModels for your views. There should be an abstraction from your Views to your Domain Models. In the demos/tutorials they show it all pretty and easy by simply strongly typing the Views to Domain Models but that's not a good strategy. The views should not be dependent on the business objects.

您应该实现大卫·格伦提出的解决方案为您目前的情况,也为所有其他的意见,即使需要域模型映射到另一个视图模型类。

You should implement David Glenn's proposed solution for your current scenario and also for all other views even if requires mapping the domain model to to another view model class.

编辑:

如果你可以说一个顶级菜单> TopMenu.aspx
而且你有多个局部视图里面> StudentMenu.ascx ResultMenu.ascx

If you have lets say a top Menu > TopMenu.aspx And you have multiple partial views inside it > StudentMenu.ascx, ResultMenu.ascx

您将创建顶级菜单视图模型> TopMenuViewModel.cs
而你也将创造局部视图视图模型> StudentMenuViewModel ResultMenuViewModel 等。

You will create a View Model for Top Menu > TopMenuViewModel.cs And you will also create view models for partial views > StudentMenuViewModel , ResultMenuViewModel etc.

和您的TopMenuViewModel将同时拥有>

and your TopMenuViewModel will have both >

class TopMenuViewModel 
{
   //all the stuff required in TopMenu.aspx
   StudentMenuViewModel studentvm;
   ResultMenuViewModel resultvm;
}

TopMenu.aspx 渲染局部时,你会通过有关的视图模型>

and in TopMenu.aspx when rendering the partial you will pass the relevant view model >

Html.RenderPartial('StudentView',Model.studentvm)

希望这是有道理的。

这篇关于在单个视图中的多个模型(C#MVC3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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