Two models in one view in ASP MVC 3(转http://stackoverflow.com/questions/5550627/two-models-in-one-view-in-asp-mvc-3)

Coordinator
Sep 28, 2011 at 9:31 AM
<input type="hidden" value="5550627" /> up vote 3 down vote favorite
2
share [fb] share [tw]

I have 2 models:

public class Person
{
    public int PersonID { get; set; }
    public string PersonName { get; set; }
}
public class Order
{
    public int OrderID { get; set; }
    public int TotalSum { get; set; }
}

I want edit objects of BOTH classes in SINGLE view, so I need something like:

@model _try2models.Models.Person
@model _try2models.Models.Order

@using(Html.BeginForm())
{
    @Html.EditorFor(x => x.PersonID)
    @Html.EditorFor(x => x.PersonName)
    @Html.EditorFor(x=>x.OrderID)
    @Html.EditorFor(x => x.TotalSum)
}

This, of course, don't work: Only one 'model' statement is allowed in a .cshtml file. May be there is some workaround?

asked Apr 5 at 10:49
Smarty
605
1  
Does my answer help you? – Andi Apr 5 at 12:13
feedback
<input type="hidden" value="5550646" /> up vote 8 down vote accepted

Create a parent view model that contains both models.

public class MainPageModel{
    public Model1 Model1{get; set;}
    public Model2 Model2{get; set;}
}

This way you can add additional models at a later date with very minimum effort.

answered Apr 5 at 10:51
Andi
2,879931
feedback
<input type="hidden" value="5550667" /> up vote 1 down vote

You can use the presentation pattern http://martinfowler.com/eaaDev/PresentationModel.html

This presentation "View" model can contain both Person and Order, this new
class can be the model your view references.

answered Apr 5 at 10:53
James Kyburz
1,63816
feedback
<input type="hidden" value="5550796" /> up vote 1 down vote

Check out this video on asp.net, Chris Pels explains the basics of using View Models.

answered Apr 5 at 11:03
geepie
35210
feedback
<input type="hidden" value="7558510" /> up vote 1 down vote

Another option which doesn't have the need to create a custom Model is to use a Tuple<>.

@model Tuple<Person,Order>

It's not as clean as creating a new class which contains both, as per Andi's answer, but it is viable.

answered yesterday
Bobson
1878
feedback
<input type="hidden" value="5550749" /> up vote 0 down vote

If you are a fan of having very flat models, just to support the view, you should create a model specific to this particular view...

public class EditViewModel
    public int PersonID { get; set; }
    public string PersonName { get; set; }
    public int OrderID { get; set; }
    public int TotalSum { get; set; }
}

Many people use AutoMapper to map from their domain objects to their flat views.

The idea of the view model is that it just supports the view - nothing else. You have one per view to ensure that it only contains what is required for that view - not loads of properties that you want for other views.

answered Apr 5 at 10:59
Sohnee
14.5k11340
feedback
<input type="hidden" value="7408622" /> up vote 0 down vote

Another way that is never talked about is Create a view in MSSQL with all the data you want to present. Then use LINQ to SQL or whatever to map it. In your controller return it to the view. Done.

answered Sep 13 at 21:16
jvelez
1518
Was this post useful to you?   <input class="anon-vote" style="zoom: 1;" type="button" value="Yes" />  <input class="anon-vote" style="zoom: 1;" type="button" value="No" />