c# - MVC4 using two models for 2 different related tables on one view -


this question has answer here:

i'm new mvc. have visual studio 2010 , mvc 4.

as articles read, have create model class, , add controller generate (create, delete, details, edit , index views).

now suppose have 2 related tables, like: company , companybranches.

i can create model, controller , views each 1 individually can't combine 2 views (i want modify details view of company, display related companybranches on it.).

how can this? knowing tried add reference company branches model company details view, looks adding 2 models not allowed on mvc.

you can create model or viewmodel this:

public class viewmodel {    public company mycompany { get; set;}    public companybranches mycompanybranches { get; set;}     //if have multiple items, can this:    public ilist<companybranches> lstcompanybranches { get; set;} } 

then pass view:

public actionresult create() {    viewmodel model = new viewmodel();    model.mycompany = //populate company details class    model.mycompanybranches = //populate companybranchess class     return view(model); //return view 2 classes on 1 class } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -