简体   繁体   English

将两个模型合并到asp.net mvc4中的单个视图中

[英]Merging two models into the single view in asp.net mvc4

@model .Models.somemodel1. this is one  view data.

@model .Models.somemodel2. this is another view data.

How can I merge both models into a single view? 如何将两个模型合并到一个视图中?

Create a viewmodel wrapper class then use it in a view. 创建一个viewmodel包装类,然后在视图中使用它。 Read What is ViewModel in MVC? 阅读MVC中的ViewModel是什么?

public class MyViewModel
{
 public SomeModel1 someModel1{get;set;}
 public SomeModel2 someModel2{get;set;}
}

In your View 在你的视图中

 @model MyViewModel

 <div>Model.someModel1.Property</div>

You can use Tuple 你可以使用元组

In View 在视图中

@model  Tuple<Model1, Model2>

// You can access the model values like
@Model.Item1.FirstName
@Model.Item2.Mark

Controller 调节器

Model1 m1 = new Model1();
Model2 m2= new Model2();
var tuple = new Tuple<Model1,Model2>(m1,m2);
return View(tuple);

For more details 更多细节

http://msdn.microsoft.com/en-us/library/dd268536(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/dd268536(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM