I am having difficulties displaying a list of data that incoporates two database objects.
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.
This is my viewmodel
namespace CustomerDatabase.WebUI.Models
{
public class CustomerSitesListViewModel
{
public IList<CustomerSite> CustomerSites { get; set; }
public PagingInfo PagingInfo { get; set; }
public IList<Customer> customers { get; set; }
}
}
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
<%@ Page T开发者_开发问答itle="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CustomerDatabase.WebUI.Models.CustomerSitesListViewModel> " %>
<% foreach (var customer in Model.customers) { %>
<%:customer.CustomerName%></li>
<%} %>
<% foreach (var customerSite in Model.CustomerSites) { %>
<% Html.RenderPartial("CustomerSiteSummary", customerSite); %>
<%} %>
This is my partial view
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CustomerDatabase.Domain.Entities.CustomerSite>" %>
<div class="item">
<div class="customer-list-item">
<%: Model.AddressLine1%>
<%: Model.AddressLine2%>
<%: Model.AddressLine3%>
<%: Model.Town%>
<%: Model.County%>
<%: Model.Postcode%>
<%: Model.ContactName%>
<%: Model.ContactNo%>
<%: Model.ContactEmail%>
This is my controller code which sets up the two lists
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)
}
}
}
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.
<% foreach **(var customer in Model.customers)** { %>
Thanks for any advice anyone may be able to offer.
Sorry, but your question is still a bit vague for me, but in any case, it seems you want some data structure like this.
namespace CustomerDatabase.WebUI.Models
{
public class CustomerSitesListViewModel
{
public PagingInfo PagingInfo { get; set; }
public IList<Customer> customers { get; set; }
}
public class Custmoer
{
public IList<CustomerSite> CustomerSites { get; set; }
//other customer properties
}
}
You can easily build that kind of view model in the controller and then adjust your views like that. You can also use a grid or build one with a foreach loop for the list of data (like you are doing already).
精彩评论