Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.net MVC2 Accessing two tables from a partial view
    primarykey
    data
    text
    <p>Im looking for some advice on the best way to access two database tables from one partial view. </p> <p>I have a view model that includes two lists of objects, customers and customer sites,</p> <pre><code>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>I have a partial view that inherits the view model</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;CustomerDatabase.WebUI.Models.CustomerSitesListViewModel&gt;" %&gt; &lt;ul id="list-of-sites"&gt; &lt;li&gt;&lt;%: Model.AddressLine1 %&gt;&lt;/li&gt; &lt;li&gt;&lt;%: Model.AddressLine2%&gt;&lt;/li&gt; &lt;li&gt;&lt;%: Model.AddressLine3%&gt;&lt;/li&gt; </code></pre> <p></p> <p>Then i have a for each loop in the main view that inserts the partial view</p> <pre><code>&lt;% foreach (var customerSite in Model.CustomerSites) { %&gt; &lt;% %&gt; &lt;% Html.RenderPartial("CustomerSiteSummary", customerSite); %&gt; &lt;%} %&gt; </code></pre> <p>Now in the partial view id like to be able to access properties for both objects both customer and customer site, i thought i would be able to access them like this</p> <pre><code>&lt;%Html.DisplayFor(modelItem =&gt; Model.customers.CustomerName)%&gt; &lt;%Html.DisplayFor(modelItem =&gt; Model.CustomerSite.AddressLine1)%&gt; </code></pre> <p>However the furthest i can get is </p> <pre><code>Model.customers </code></pre> <p>Is there anyway to access the customer properties, i.e. customers.CustomerName, </p> <p>This is one aspect of MVC that i really dont understand, ive been stuck with this for over a week now, would appreciate any advice anyone can offer.</p> <p>Regards</p> <p>Liam</p> <p>Update=======</p> <p>Ive added the following view models</p> <p>Customer View Model</p> <pre><code>public class CustomerViewModel { public int Id { get; set; } public string CustomerName { get; set; } public string PrimaryContactName { get; set; } public string PrimaryContactNo { get; set; } public SiteViewModel Site { get; set; } } </code></pre> <p>Site View Model</p> <pre><code>public class SiteViewModel { public int Id { get; set; } public int CustomerId { get; set; } public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } </code></pre> <p>Master view model</p> <pre><code>public class CustomerSitesListViewModel { public IList&lt;CustomerSite&gt; CustomerSites { get; set; } public PagingInfo PagingInfo { get; set; } public IList&lt;Customer&gt; customers { get; set; } public IEnumerable&lt;CustomerViewModel&gt; Customers { get; set; } } </code></pre> <p>Ive strongly typed the view to the CustomerSitesListViewModel</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;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt;Customer Sites&lt;/h2&gt; &lt;%= Html.DisplayFor(x =&gt; x.Customers) %&gt; &lt;div class="pager"&gt; &lt;%: Html.PageLinks(Model.PagingInfo, x =&gt; Url.Action("List", "CustomerSites", new { page = x }))%&gt; </code></pre> <p> </p> <p>Ive added a template into Views Shared/DisplayTemplates</p> <p>CustomerSitesListViewModel.ascx</p> <pre><code>&lt;%= Html.DisplayFor(x =&gt; x.Customers.) %&gt; </code></pre> <p>This still wont let me access the properties, i can access both the customers and customersites objects but not the properties, so the furthest it will let me go is x.Customers or x.CustomerSites</p> <p>This is my controller that returns the two lists of customers and sites</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 </code></pre> <p>Can you offer any advice on where i am going wrong?</p> <p>Many Thanks</p> <p>Liam</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.
    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