Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you help with this MVC ViewModel issue?
    text
    copied!<p>I have a problem with an MVC view that I just cannot seem to solve. Here it is.</p> <p>1) I have an index view that displays a list of retailers with data from the retailers table. So far so good.</p> <p>2) I also want to include the retailer categories for each retailer which are stored in a RetailersCategories table where each retailer can have multiple categories</p> <p>I have tried a few things but cannot seem to make this work. The closest I came to what I wanted was using a view model. I have included the code below. </p> <p>I actually get the right data back but I get all of the retailer records and then all of the category records. </p> <p>What I need is one retailer record at a time with all of the categories that relate to that retailer.</p> <p>Can anyone show me how I can achieve this?</p> <pre><code>//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&lt;RetailersShipping&gt; RetailerShipping { get; set; } public IEnumerable&lt;RetailersCategory&gt; RetailerCategories { get; set; } public RetailersIndexViewModel(IEnumerable&lt;RetailersShipping&gt; retailer, IEnumerable&lt;RetailersCategory&gt; retailercategory) { this.RetailerShipping = retailer; this.RetailerCategories = retailercategory; } } //IndexView &lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Inner.Master" Inherits="System.Web.Mvc.ViewPage&lt;RetailersIndexViewModel&gt;" %&gt; &lt;% Html.RenderPartial("RetailerSummaryPartial", this.ViewData.Model.RetailerShipping); %&gt; &lt;div id="retailer_index_categories"&gt; &lt;% foreach (RetailersCategory category in ViewData.Model.RetailerCategories) {%&gt; &lt;% Html.RenderPartial("RetailerCategoryPartial", category); %&gt; &lt;% } %&gt; // RetailerSummaryPartial &lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;IEnumerable&lt;RetailersShipping&gt;&gt;" %&gt; &lt;div id="retailer_partial_summary"&gt; &lt;% foreach (var retailer in Model) { %&gt; &lt;div id="retailer_index_image"&gt; &lt;img src="&lt;%=Html.Encode(retailer.Retailer.Country.Image) %&gt;" title="&lt;%= Html.Encode(retailer.Retailer.Name) %&gt;&gt;" alt="&lt;%= Html.Encode(retailer.Retailer.Name) %&gt;" class="main-image" /&gt; &lt;br /&gt; &lt;/div&gt; &lt;div id="retailer_index_logo"&gt; &lt;img src="&lt;%=Html.Encode(retailer.Retailer.Logo) %&gt;" title="&lt;%= Html.Encode(retailer.Retailer.Name) %&gt;&gt;" alt="&lt;%= Html.Encode(retailer.Retailer.Name) %&gt;" class="main-image" /&gt; &lt;/div&gt; &lt;div id="retailer_index_name_comment"&gt; &lt;%= Html.Encode(retailer.Retailer.Name)%&gt;&lt;br /&gt; &lt;span&gt;&lt;%if (retailer.Retailer.CountryId == retailer.Retailer.CountryId) %&gt; &lt;%= Html.Encode(retailer.Retailer.LocalComment)%&gt; &lt;%= Html.Encode(retailer.Retailer.IntComment)%&gt; &lt;/span&gt; &lt;/div&gt; &lt;% } %&gt; &lt;/div&gt; //RetailerCategoryPartial &lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;RetailersCategory&gt;" %&gt; &lt;div id="retailer_index_categories"&gt; &lt;%= Html.Encode(Model.Category.CategoryName) %&gt; &lt;/div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload