0
@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?

Nalaka526
  • 11,278
  • 21
  • 82
  • 116
pavani
  • 11
  • 4

2 Answers2

3

Create a viewmodel wrapper class then use it in a view. Read What is ViewModel in MVC?

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

In your View

 @model MyViewModel

 <div>Model.someModel1.Property</div>
Community
  • 1
  • 1
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
  • Thanks for explanations. When using ViewModel, I cannot retrieve Display Names typed on Domain Model. If I have to define them on DomainModel as well what if I use several viewmodels from the same Domain model? In that case I have to define this as `[Display(Name = "Name")]` multiple times. Is that true? – Jack Jun 25 '15 at 10:40
  • @Christof, Hope you have defined `[Display(Name = "Name")]` in the view model object you bound to your view. If its not working, please feel free to ask a new question in stackoverflow, so it would be easy for us to see the code and understand the problem. – Murali Murugesan Jun 25 '15 at 11:23
  • Thanks for your reply. Could you have a look at [this](http://stackoverflow.com/questions/31048530/is-it-possible-to-reuse-the-dataannotations-in-viewmodel/31048731?noredirect=1#comment50118981_31048731) question pls? – Jack Jun 25 '15 at 11:25
0

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

Golda
  • 3,823
  • 10
  • 34
  • 67