You must Sign In to post a response.
  • Category: ASP.Net MVC

    Why to pass mutliple models to a view

    Hi,

    Can anyone tell me why we need to pass multiple models to a view in MVC?

    Regards,

    RAJAN CRP
  • #766131
    Basically model is a class that maps to the data relation (table) and potentially bridge tables (e.g. for many to many relations).
    you should keep your data model, the algorithms. For example if you write a spreadsheet application, you would keep the data structure of your spreadsheet. You would have the computation engine in your model, you would have the code to save and load your spreadsheet in your model. These model class could potentially be reused in other applications, for example if you have code to do compression of data.
    so surely model is more helpful.
    may be you can check multiple model in single view in following link
    http://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766139
    In MVC we can not pass multiple models directly into a view. (M->V).

    You can send core model into view. If you want to send other models you can send via viewbag and viewdata.
    In the main view you can get data from the viewdata or viewbag and assign data in the view.

    The best approach is you have to create new mode based on the view assign all the required models in to it and pass to the view

    By Nathan
    Direction is important than speed

  • #766219
    You can achieve this by creating a new model and refer other two models in it.

    Say for example you have two models "Model1" and "Model2" you want to pass this two models to a view. In order to achieve this you just want to create a new model "Model3" and create a property with for the "Model1" and "Model2".

    Now you can pass the Model3 to the view and you can refer Model1 and Model2 over there.

    Hope this helps you, let me know if you need clarifications on this.

  • #766258
    Hi
    You can use
    dynamic mymodel = new ExpandoObject();
    and add New Properties to that mymodel
    e.q mymodel.Student=collection of student
    mymodel.Teacher=collection of teacher
    also you can refer following link for more details

    http://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/

    Hope this will help you
    Thanks
    Umesh Bhosale


  • Sign In to post your comments