locked
Multiple Models in View RRS feed

  • Question

  • User1904516115 posted

    Why multiple Models not allowed directly(by design) in View for ASP.NET MVC application?

    Saturday, August 10, 2019 6:15 PM

All replies

  • User475983607 posted

    Why multiple Models not allowed directly(by design) in View for ASP.NET MVC application?

    Multiple models are allowed in a View.  Simply pass a model that has many models or take advantage of the ViewBag.  

    Sunday, August 11, 2019 11:23 AM
  • User1120430333 posted
    You can have multiple models in a view by simply using a view model.
    Sunday, August 11, 2019 11:32 AM
  • User-2054057000 posted

    There is a trick to do it. Design a model that contains other models as its properties (since models are nothing but class).

    Sunday, August 11, 2019 11:36 AM
  • User1520731567 posted

    Hi vinodkpasi,

    You could use multiple Models in mvc.

    I suggest you could select data as a new ViewModel which contains some properties from other models

    or use a DTO Model which contains other models directly.

    For example:

    public class SModel
    {
         public int Id {get; set;}
         public string Name{ get; set; }
         ...
    }
    
    public class SModel2
    {
         public int Id {get; set;}
         public string Name2{ get; set; }
         ...
    }

    Create a viewmodel:

    public class SViewModel
    {
         public int Id {get; set;}
         public string Name {get; set;}
         public string Name2{get; set;}
    }

    or Create a DTO model:

    public class SDTOModel
    {
         public int Id {get; set;}
         public SModel model1{get; set;}
         public SModel2 model2{get; set;}
    }

    And then invoke strongly type your view:

    @model TestApplication1.xxx.xxxx.SViewModel
    

    or:

    @model TestApplication1.xxx.xxxx.SDTOModel

    Best Regards.

    Yuki Tao

    Monday, August 12, 2019 2:32 AM
  • User1904516115 posted

    I know about ViewModel. But we have to create new class for View. By design why MVC is limiting us to create new class?

    Saturday, September 14, 2019 5:26 PM
  • User475983607 posted

    I know about ViewModel. But we have to create new class for View. By design why MVC is limiting us to create new class?

    What limitation?  Multiple models are allowed.  ViewBag, TempData, and ViewData are dynamic methods for passing types.  

    Saturday, September 14, 2019 5:32 PM
  • User1120430333 posted
    What limitations are you talking about? Maybe, you don't understand separation of concerns in Object Oriented Programming and design concerning the MVC UI design pattern.
    Saturday, September 14, 2019 8:57 PM
  • User409696431 posted

    I assume this is a restatement of your original question, "Why [are] multiple Models not allowed directly(by design) in View for ASP.NET MVC application?"

    You'd have to ask the developers of MVC that question.  Each View can have one model.  As others have said, and as you already knew, you can create a ViewModel with the classes you need and define the relationships you need, and use that as the model.  Since that makes having more than one model per view unnecessary (and is more flexible) it's not hard to make a case that it was a valid decision.  You might disagree, of course.

    Sunday, September 15, 2019 12:07 AM