Two models in one view in ASP MVC 3

后端 未结 12 1877
日久生厌
日久生厌 2020-11-22 13:55

I have 2 models:

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


        
相关标签:
12条回答
  • 2020-11-22 14:31

    Just create a single view Model with all the needed information in it, normaly what I do is create a model for every view so I can be specific on every view, either that or make a parent model and inherit it. OR make a model which includes both the views.

    Personally I would just add them into one model but thats the way I do it:

    public class xViewModel
    {
        public int PersonID { get; set; }
        public string PersonName { get; set; }
        public int OrderID { get; set; }
        public int TotalSum { get; set; }
    }
    
    @model project.Models.Home.xViewModel
    
    @using(Html.BeginForm())
    {
        @Html.EditorFor(x => x.PersonID)
        @Html.EditorFor(x => x.PersonName)
        @Html.EditorFor(x => x.OrderID)
        @Html.EditorFor(x => x.TotalSum)
    }
    
    0 讨论(0)
  • 2020-11-22 14:33

    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.

    0 讨论(0)
  • 2020-11-22 14:34

    Beside of one view model in asp.net you can also make multiple partial views and assign different model view to every view, for example:

       @{
            Layout = null;
        }
    
        @model Person;
    
        <input type="text" asp-for="PersonID" />
        <input type="text" asp-for="PersonName" />
    

    then another partial view Model for order model

        @{
            Layout = null;
         }
    
        @model Order;
    
        <input type="text" asp-for="OrderID" />
        <input type="text" asp-for="TotalSum" />
    

    then in your main view load both partial view by

    <partial name="PersonPartialView" />
    <partial name="OrderPartialView" />
    
    0 讨论(0)
  • 2020-11-22 14:36

    In fact there is a way to use two or more models on one view without wrapping them in a class that contains both.

    Using Employee as an example model:

    @model Employee
    

    Is actually treated like.

    @{ var Model = ViewBag.model as Employee; }
    

    So the View(employee) method is setting your model to the ViewBag and then the ViewEngine is casting it.

    This means that,

    ViewBag.departments = GetListOfDepartments();
        return View(employee);
    

    Can be used like,

                @model  Employee
            @{
                    var DepartmentModel = ViewBag.departments as List<Department>;
            }
    

    Essentially, you can use whatever is in your ViewBag as a "Model" because that's how it works anyway. I'm not saying that this is architecturally ideal, but it is possible.

    0 讨论(0)
  • 2020-11-22 14:37

    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.

    0 讨论(0)
  • 2020-11-22 14:47

    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.

    0 讨论(0)
提交回复
热议问题