【问题标题】:Can you help with this MVC ViewModel issue?你能帮忙解决这个 MVC ViewModel 问题吗?
【发布时间】:2010-11-30 21:40:52
【问题描述】:

我的 MVC 视图有一个问题,我似乎无法解决。在这里。

1) 我有一个索引视图,它显示了零售商列表中的零售商表中的数据。到目前为止一切顺利。

2) 我还想包含每个零售商的零售商类别,它们存储在一个 RetailersCategories 表中,每个零售商可以有多个类别

我已经尝试了一些东西,但似乎无法完成这项工作。我最接近我想要的东西是使用视图模型。我已经包含了下面的代码。

我实际上得到了正确的数据,但我得到了所有的零售商记录,然后是所有的类别记录。

我需要的是一个零售商记录,其中包含与该零售商相关的所有类别。

谁能告诉我如何做到这一点?

//Controller   

public ActionResult Index(int? page, int country)
{
    var viewdata = new retailersIndexViewModel(_retailerRepository.GetAllRetailersByCountry(country),  _retailerRepository.GetRetailerCategories());
    return View(viewdata);       
}

// ViewModel

public class RetailersIndexViewModel
{        
    public IEnumerable<RetailersShipping> RetailerShipping { get; set; }
    public IEnumerable<RetailersCategory> RetailerCategories { get; set; }

    public RetailersIndexViewModel(IEnumerable<RetailersShipping> retailer, IEnumerable<RetailersCategory> retailercategory)
    {
        this.RetailerShipping = retailer;
        this.RetailerCategories = retailercategory;
    }
}


//IndexView

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Inner.Master" Inherits="System.Web.Mvc.ViewPage<RetailersIndexViewModel>" %>

<% Html.RenderPartial("RetailerSummaryPartial", this.ViewData.Model.RetailerShipping); %>
<div id="retailer_index_categories">

<%
    foreach (RetailersCategory category in ViewData.Model.RetailerCategories) 
    {%>
        <% Html.RenderPartial("RetailerCategoryPartial", category); %>
    <% } %>


// RetailerSummaryPartial

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<RetailersShipping>>" %>

<div id="retailer_partial_summary">
<% foreach (var retailer in Model)
{ %>
    <div id="retailer_index_image">
        <img src="<%=Html.Encode(retailer.Retailer.Country.Image) %>" title="<%= Html.Encode(retailer.Retailer.Name) %>>" alt="<%= Html.Encode(retailer.Retailer.Name) %>" class="main-image" />
        <br />
    </div>
    <div id="retailer_index_logo">
        <img src="<%=Html.Encode(retailer.Retailer.Logo) %>" title="<%= Html.Encode(retailer.Retailer.Name) %>>" alt="<%= Html.Encode(retailer.Retailer.Name) %>" class="main-image" />
    </div>
    <div id="retailer_index_name_comment">
        <%= Html.Encode(retailer.Retailer.Name)%><br />
        <span><%if (retailer.Retailer.CountryId == retailer.Retailer.CountryId) %>
                <%= Html.Encode(retailer.Retailer.LocalComment)%>
                <%= Html.Encode(retailer.Retailer.IntComment)%>
        </span>
    </div>

<% } %>
</div>


//RetailerCategoryPartial

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<RetailersCategory>" %>
<div id="retailer_index_categories">
    <%= Html.Encode(Model.Category.CategoryName) %>
</div>

【问题讨论】:

  • 您是否有机会使用 Entity Framework 4?
  • 正在寻找有关如何构建 ViewModel 或如何将其与当前视图模型一起破解的指导?理想情况下,您只需在每个零售商上都有一个类别列表...而不是尝试在视图中加入类别...
  • 嗨 dotjoe。如果可行,很高兴使用类别列表解决方案。你能解释一下吗?

标签: asp.net asp.net-mvc views viewmodel viewdata


【解决方案1】:

我会重构您的视图模型。如果您要在集合中表示一个集合,那么您的视图模型最好能反映这一点。

public class RetailersIndexViewModel {

    public IEnumerable<RetailersShippingViewModel> RetailerShippings { get; set; }

    public RetailersIndexViewModel(IEnumerable<RetailersShipping> shippings)
    {
        this.RetailerShippings = shipping;
        foreach( var shipping in shippings)
        {
            shipping.RetailerCategories = shipping.Categories // assuming Categories is referenced in your Retailer Shipping class;
        }
    }
}

public class RetailerShippingViewModel {

    public IEnumerable<RetailersCategory> RetailerCategories { get; set; }

    public RetailersIndexViewModel(IEnumerable<RetailersCategory> retailercategories)
    {
        this.RetailerCategories = retailercategories;
    }
}

并像这样渲染它

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Inner.Master" Inherits="System.Web.Mvc.ViewPage<RetailersIndexViewModel>" %>

   <% foreach(var shipping in Model.RetailerShippings)
      {
          Html.RenderPartial("RetailerSummaryPartial", shipping); 
      }%>

在您的 RetailerSummaryPartial 而不是索引视图中调用它

<%
    foreach (var category in ViewData.Model.RetailerCategories) 
       {%>
    <% Html.RenderPartial("RetailerCategoryPartial", category); %>
    <% } %>

【讨论】:

    【解决方案2】:

    尝试使用其中一种 ORM 框架(例如 NHibernate、ADO.NET 实体框架、Linq to SQL 等),然后与您的实体进行有效的映射 db 架构,然后您可以获得一个类别有多个零售商,构建 ViewModel 并绑定到 View。如果想让它复杂一点,可以自定义Model Binder,用于View和Controller之间的绑定。恕我直言。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 2014-03-01
      相关资源
      最近更新 更多