ASP.NET MVC2在强类型视图中显示2个数据列表

编程入门 行业动态 更新时间:2024-10-28 07:32:08
本文介绍了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个数据列表

本文发布于:2023-11-16 00:02:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   类型   数据   列表   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!