【问题标题】:MVC3 Show information from more than 1 entity in 1 viewMVC3 在 1 个视图中显示来自多个实体的信息
【发布时间】:2011-03-24 11:31:09
【问题描述】:

对于我的管理应用程序,我的模型文件夹中有一个 dbml 文件,其中包含实体(例如个人、公司、地址、联系人......)

在 mvc 的帮助下,我可以处理到目前为止的所有 CRUD 操作,但只能来自 1 个实体。 现在我想在 1 视图中显示人员实体的信息以及联系人和地址实体的相应信息。实体已经与正确的关系连接。

我怎样才能有效地做到这一点?

【问题讨论】:

    标签: c# asp.net visual-studio-2010 asp.net-mvc-3 view


    【解决方案1】:

    您需要创建一个 ViewModel,用于保存您在实际视图中所需的每个实体的信息。

    例如,我有一个名为 PublicationsController 的控制器,它将在我的 Web 应用程序中构建一个目录页面。一个 ActionResult 需要为我的 View 提供一个名为 PublicationsListViewModel 的 ViewModel。

    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; }
    }
    

    如您所见,PublicationsListViewModel 只是需要传递给我的视图的每个实体的容器。

    我的 PublicationsController 如下所示:

    控制器:

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

    我的视图看起来像这样

    查看:

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

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多