asp.net mvc - How do I display data from multiple tables in a single MVC view -


i having hard time solving following mvc view.

my goal display data multiple tables in single mvc view. bulk of data comes table called retailers. have table called retailercategories stores retailerid retailers table , categoryid linking category table.

note there multiple records each retailerid in retailercategories table.

in view want show list of retailers , each retailer want show list of categories applicable them.

what best way accomplish this? of things have tried covered in can mvc viewmodel issue?

this not appear right approach.

you need view model tailored needs of view. when defining view models shouldn't thinking in terms of tables. sql tables have absolutely no meaning in view. think in terms of information need show , define view models accordingly. use automapper convert between real models , view model have defined.

so forget said tables , focus on following sentence:

in view want show list of retailers , each retailer want show list of categories applicable them.

this sentence explains need. once know need go ahead , modelize it:

public class categoryviewmodel {     public string name { get; set; } }  public class retailerviewmodel {     public ienumerable<categoryviewmodel> categories { get; set; } } 

now type view ienumerable<retailerviewmodel>. here easy-peasy want in view:

showing list of retailers each retail having list of associated categories.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -