私は解決できないMVCビューに問題があります。ここにあります。このMVC ViewModelの問題を助けてくれますか?
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>
のごRetailerSummaryPartialでとても
呼び出し、このようにそれをレンダリングあなたはEntity Frameworkの4を使用して、任意の偶然か? – gligoran
実際にlinq to sql – Cunners
ViewModelの構造化方法や、現在のビューモデルと一緒にハックする方法についてのガイダンスをお探しですか?理想的には、ビューのカテゴリに参加しようとするのではなく、各小売業者のカテゴリリストを持つだけです... – dotjoe