MVCビューに問題があり、解決できないようです。ここにあります。
1)小売業者テーブルのデータを含む小売業者のリストを表示するインデックスビューがあります。ここまでは順調ですね。
2)各小売業者が複数のカテゴリを持つことができるRetailersCategoriesテーブルに格納されている各小売業者の小売業者カテゴリも含めたいと思います
私はいくつかのことを試しましたが、これを機能させることができないようです。私が欲しかったものに最も近いのは、ビューモデルを使用することでした。以下のコードを含めました。
私は実際に正しいデータを取得しますが、すべての小売業者のレコードを取得してから、すべてのカテゴリのレコードを取得します。
私が必要としているのは、その小売業者に関連するすべてのカテゴリを含む、一度に1つの小売業者のレコードです。
誰かが私がこれを達成する方法を教えてもらえますか?
//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>