[英]MVC3 Show information from more than 1 entity in 1 view
For my management application, i have in my modelfolder a dbml file with entities (ex. person, company, address, contact, ...) 对于我的管理应用程序,我在模型文件夹中有一个带有实体(例如人,公司,地址,联系人等)的dbml文件。
With help of mvc i can handle all CRUD operations so far, but only from 1 entity. 借助mvc,我可以处理到目前为止的所有CRUD操作,但只能处理1个实体。 Now I want to show in 1 view information of the person entity and also the corresponding information of contact and address entity.
现在,我想在1中显示人员实体的视图信息以及联系人和地址实体的相应信息。 The entities are already connected with the correct relations.
实体已经连接了正确的关系。
How can I do this efficiently ? 如何有效地做到这一点?
You need to create a ViewModel that will be used to hold information from each entity you need within your actual View. 您需要创建一个ViewModel,该模型将用于保存实际View中您需要的每个实体的信息。
For example, I have a controller called PublicationsController that will build a catalog page within my web application. 例如,我有一个名为PublicationsController的控制器,它将在我的Web应用程序中构建目录页面。 One ActionResult needs to provide a ViewModel called PublicationsListViewModel to my View.
一个ActionResult需要为我的View提供一个称为PublicationsListViewModel的ViewModel。
PublicationsListViewModel: PublicationsListViewModel:
public class PublicationsListViewModel
{
public IList<Publication> Publications { get; set; }
public PagingInfo PagingInfo { get; set; }
public String CurrentCategory { get; set; }
public Dictionary<String, String> SelectedItems { get; set; }
}
As you can see, the PublicationsListViewModel is just a container for each Entity that needs to be passed to my View. 如您所见,PublicationsListViewModel只是每个需要传递到我的View的实体的容器。
My PublicationsController looks like this: 我的PublicationsController看起来像这样:
Controller: 控制器:
public ActionResult List(Cart cart, string category, int page = 1)
{
Dictionary<String, String> selectedItems = new Dictionary<String, String>();
foreach (var line in cart.Lines)
{
selectedItems.Add(line.Publication.PDFID.ToString(), line.Quantity.ToString());
}
foreach (var line in cart.Lines)
{
ViewData.Add(line.Publication.PDFID.ToString(), line.Quantity.ToString());
}
var publicationsToShow = (category == null)
? publicationsRepository.Publications
: publicationsRepository.Publications.Where(x => x.Category.Equals(category, StringComparison.CurrentCultureIgnoreCase));
var viewModel = new PublicationsListViewModel
{
Publications = publicationsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = publicationsToShow.Count()
},
CurrentCategory = category,
SelectedItems = selectedItems
};
return View(viewModel); // Passed to view as ViewData.Model (or simply Model)
}
And my View looks like this 我的视图看起来像这样
View: 视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<myNameSpace.WebUI.Models.PublicationsListViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Publications : <%: Model.CurrentCategory ?? "All Publications" %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%
foreach (var publication in Model.Publications)
{
Html.RenderPartial("PublicationSummary", publication, ViewData);
}
%>
<div class="pager">
<%: Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x, category = Model.CurrentCategory }))%>
</div>
</asp:Content>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.