Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC2 displaying 2 lists of data in a strongly typed view
    primarykey
    data
    text
    <p>I am having difficulties displaying a list of data that incoporates two database objects.</p> <p>My view is strongy typed to a view model that includes a list of customers and a list of customer sites (each stored in their own table). Im currently using two for each statments to render both lists, but really i need one list that contains both objects, in a nutshell its a list of customer sites that also contains customer names from the customer table. </p> <p>This is my viewmodel</p> <pre><code>namespace CustomerDatabase.WebUI.Models { public class CustomerSitesListViewModel { public IList&lt;CustomerSite&gt; CustomerSites { get; set; } public PagingInfo PagingInfo { get; set; } public IList&lt;Customer&gt; customers { get; set; } } } </code></pre> <p>This is the code im currently using in the view, the customer sites are contained in a partial view, it would be ideal if i could add the customer name in the partial view but the only way i can work out how to access both objects is to use two for each statements</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;CustomerDatabase.WebUI.Models.CustomerSitesListViewModel&gt; " %&gt; &lt;% foreach (var customer in Model.customers) { %&gt; &lt;%:customer.CustomerName%&gt;&lt;/li&gt; &lt;%} %&gt; &lt;% foreach (var customerSite in Model.CustomerSites) { %&gt; &lt;% Html.RenderPartial("CustomerSiteSummary", customerSite); %&gt; &lt;%} %&gt; </code></pre> <p>This is my partial view</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;CustomerDatabase.Domain.Entities.CustomerSite&gt;" %&gt; &lt;div class="item"&gt; &lt;div class="customer-list-item"&gt; &lt;%: Model.AddressLine1%&gt; &lt;%: Model.AddressLine2%&gt; &lt;%: Model.AddressLine3%&gt; &lt;%: Model.Town%&gt; &lt;%: Model.County%&gt; &lt;%: Model.Postcode%&gt; &lt;%: Model.ContactName%&gt; &lt;%: Model.ContactNo%&gt; &lt;%: Model.ContactEmail%&gt; </code></pre> <p> </p> <p>This is my controller code which sets up the two lists</p> <pre><code>public ViewResult List([DefaultValue(1)] int page) { var customerSitesToShow = customerSiteRepository.CustomerSites; var customersToShow = customerRepository.Customers; var viewModel = new CustomerSitesListViewModel { CustomerSites = customerSitesToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(), customers = customersToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = customerSitesToShow.Count() } }; return View(viewModel); //Passed to view as ViewData.Model (or simply model) } } } </code></pre> <p>Is there another way to accesss both objects from the view without the for each statment so i can merge the two lists together in the partial view. </p> <pre><code>&lt;% foreach **(var customer in Model.customers)** { %&gt; </code></pre> <p>Thanks for any advice anyone may be able to offer. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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