ASP.NET MVC2在强类型视图中显示2个数据列表 [英] ASP.NET MVC2 displaying 2 lists of data in a strongly typed view

查看:283
本文介绍了ASP.NET MVC2在强类型视图中显示2个数据列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难显示包含两个数据库对象的数据列表.

I am having difficulties displaying a list of data that incoporates two database objects.

我的视图强力键入一个视图模型,该模型包括一个客户列表和一个客户站点列表(每个站点都存储在自己的表中).我目前为每个语句使用两个来呈现两个列表,但实际上我需要一个包含两个对象的列表,简而言之,它是一个客户站点列表,其中也包含来自customer表的客户名称.

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.

这是我的视图模型

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 Title="" 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); %>
<%} %>

这是我的部分看法

<%@ 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
}
}

您可以在控制器中轻松构建这种视图模型,然后像这样调整视图.您也可以使用网格或使用foreach循环构建网格以获取数据列表(就像您已经在做的那样).

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).

这篇关于ASP.NET MVC2在强类型视图中显示2个数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆