asp.net mvc 3 - How to build a complex View Model -


i wondering if me building of view model...

i have 3 parent models, users, profiles, , projects

on view, pass model has user's information, profile information, , projects information...

so far view model looks this...

using system; using system.collections.generic; using system.linq; using system.web;  namespace prodevportmvc3.viewmodels {     public class portfolioviewmodel     {         public int profileid { get; set; }         public int userid { get; set; }         public string profilefirstname { get; set; }         public string profilelastname { get; set; }         public string profileemail { get; set; }         public nullable<long> profilecontactno { get; set; }         public string profilecity { get; set; }         public string profilestate { get; set; }         public string profilecountry { get; set; }         public string profilephotopath { get; set; }         public string profilephotoname { get; set; }         public string profilebio { get; set; }         public string profilemissionstatement { get; set; }         public string profileprivacysetting { get; set; }         public ienumerable<profileblog> profileblogs { get; set; }         public ienumerable<profileforum> profileforums { get; set; }         public ienumerable<project> projects { get; set; }     } } 

the problem projects model/table has one-to-many child tables... , need of information in view model... here child tables...

projectcodesamples projectdownloads projectscreenshots projecttechnologiesuseds

here controller action method far...

public actionresult index(int id) {     var profile = db.profiles.singleordefault(p => p.userid == id);     var profileblogs = db.profileblogs.where(p => p.profileid == profile.profileid);     var profileforums = db.profileforums.where(p => p.profileid == profile.profileid);     var projects = db.projects.where(p => p.profileid == profile.profileid);     var viewmodel = new portfolioviewmodel     {         profilefirstname = profile.profilefirstname,         profilelastname = profile.profilelastname,         profileemail = profile.profileemail,         profilecontactno = profile.profilecontactno,         profilecity = profile.profilecity,         profilestate = profile.profilestate,         profilecountry = profile.profilestate,         profilephotopath = profile.profilephotopath,         profilephotoname = profile.profilephotoname,         profilebio = profile.profilebio,         profilemissionstatement = profile.profilemissionstatement,         profileprivacysetting = profile.profileprivacysetting,         profileblogs = profileblogs.where(p => p.profileid == profile.profileid),         profileforums = profileforums.where(p => p.profileid == profile.profileid),         projects = projects.where(p => p.profileid == profile.profileid)     };     return view(viewmodel); } 

so, how continue or do here?

edit: forgot add project child tables have foreign key projectid connect them projects table...

here final view code work...

@foreach (var item in model.projects) {     <p>     @html.displayfor(modelitem => item.projectname)     </p>      foreach (var subitem in item.projectdownloads)     {         <pre>         @html.displayfor(modelitem => subitem.projectdownloadpath)         </pre>     } } 

since use project model in viewmodel can access project's child tables in view.

for example, should able following:

foreach(var item in model.projects) {     foreach(var download in item.projectdownloads)     {         ...     } } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -