asp.net - Can you help with this MVC ViewModel issue? -


i have problem mvc view cannot seem solve. here is.

1) have index view displays list of retailers data retailers table. far good.

2) want include retailer categories each retailer stored in retailerscategories table each retailer can have multiple categories

i have tried few things cannot seem make work. closest came wanted using view model. have included code below.

i right data of retailer records , of category records.

what need 1 retailer record @ time of categories relate retailer.

can show me how can achieve this?

//controller     public actionresult index(int? page, int country) {     var viewdata = new retailersindexviewmodel(_retailerrepository.getallretailersbycountry(country),  _retailerrepository.getretailercategories());     return view(viewdata);        }  // viewmodel  public class retailersindexviewmodel {             public ienumerable<retailersshipping> retailershipping { get; set; }     public ienumerable<retailerscategory> retailercategories { get; set; }      public retailersindexviewmodel(ienumerable<retailersshipping> retailer, ienumerable<retailerscategory> retailercategory)     {         this.retailershipping = retailer;         this.retailercategories = retailercategory;     } }   //indexview  <%@ page language="c#" masterpagefile="~/views/shared/inner.master" inherits="system.web.mvc.viewpage<retailersindexviewmodel>" %>  <% html.renderpartial("retailersummarypartial", this.viewdata.model.retailershipping); %> <div id="retailer_index_categories">  <%     foreach (retailerscategory category in viewdata.model.retailercategories)      {%>         <% html.renderpartial("retailercategorypartial", category); %>     <% } %>   // retailersummarypartial  <%@ control language="c#" inherits="system.web.mvc.viewusercontrol<ienumerable<retailersshipping>>" %>  <div id="retailer_partial_summary"> <% foreach (var retailer in model) { %>     <div id="retailer_index_image">         <img src="<%=html.encode(retailer.retailer.country.image) %>" title="<%= html.encode(retailer.retailer.name) %>>" alt="<%= html.encode(retailer.retailer.name) %>" class="main-image" />         <br />     </div>     <div id="retailer_index_logo">         <img src="<%=html.encode(retailer.retailer.logo) %>" title="<%= html.encode(retailer.retailer.name) %>>" alt="<%= html.encode(retailer.retailer.name) %>" class="main-image" />     </div>     <div id="retailer_index_name_comment">         <%= html.encode(retailer.retailer.name)%><br />         <span><%if (retailer.retailer.countryid == retailer.retailer.countryid) %>                 <%= html.encode(retailer.retailer.localcomment)%>                 <%= html.encode(retailer.retailer.intcomment)%>         </span>     </div>  <% } %> </div>   //retailercategorypartial  <%@ control language="c#" inherits="system.web.mvc.viewusercontrol<retailerscategory>" %> <div id="retailer_index_categories">     <%= html.encode(model.category.categoryname) %> </div> 

i'd refactor view models. if you're going representing collection within collection it's best view models reflect that.

public class retailersindexviewmodel {      public ienumerable<retailersshippingviewmodel> retailershippings { get; set; }      public retailersindexviewmodel(ienumerable<retailersshipping> shippings)     {         this.retailershippings = shipping;         foreach( var shipping in shippings)         {             shipping.retailercategories = shipping.categories // assuming categories referenced in retailer shipping class;         }     } }  public class retailershippingviewmodel {      public ienumerable<retailerscategory> retailercategories { get; set; }      public retailersindexviewmodel(ienumerable<retailerscategory> retailercategories)     {         this.retailercategories = retailercategories;     } } 

and render so

<%@ page language="c#" masterpagefile="~/views/shared/inner.master" inherits="system.web.mvc.viewpage<retailersindexviewmodel>" %>     <% foreach(var shipping in model.retailershippings)       {           html.renderpartial("retailersummarypartial", shipping);        }%> 

call in retailersummarypartial instead of index view

<%     foreach (var category in viewdata.model.retailercategories)         {%>     <% html.renderpartial("retailercategorypartial", category); %>     <% } %> 

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? -