问题描述
我有,我只是似乎无法来解决MVC视图的一个问题。这就是。
1)我有一个显示零售商从零售商表数据的列表的索引图。到目前为止好。
2)我还希望包括零售商类别被存储在一个RetailersCategories表,其中每个零售商可以有多个类别的每个零售商
我已经尝试了一些事情,但似乎无法使这项工作。最近我来到了我想要的是使用视图模型。我已经包括下面的code。
我真正得到正确的数据回来,但我得到的所有的零售商记录,然后所有的类别记录。
我需要的是一个零售商在记录与所有涉及到的零售商类别的时间。
谁能告诉我我怎么能做到这一点?
//控制器公众的ActionResult指数(INT?页,诠释国家)
{
VAR可视数据=新retailersIndexViewModel(_retailerRepository.GetAllRetailersByCountry(国家),_retailerRepository.GetRetailerCategories());
返回视图(可视数据);
}//视图模型公共类RetailersIndexViewModel
{
公共IEnumerable的< RetailersShipping> RetailerShipping {搞定;组; }
公共IEnumerable的< RetailersCategory> RetailerCategories {搞定;组; } 公共RetailersIndexViewModel(IEnumerable的< RetailersShipping>零售商的IEnumerable< RetailersCategory> retailercategory)
{
this.RetailerShipping =零售商;
this.RetailerCategories = retailercategory;
}
}
// IndexView<%@页面语言=C#的MasterPageFile =〜/查看/共享/ Inner.Master继承=System.Web.Mvc.ViewPage< RetailersIndexViewModel>中%GT;<%Html.RenderPartial(RetailerSummaryPartial,this.ViewData.Model.RetailerShipping); %GT;
< DIV ID =retailer_index_categories><%
的foreach(在ViewData.Model.RetailerCategories RetailersCategory类)
{%GT;
<%Html.RenderPartial(RetailerCategoryPartial类); %GT;
<%}%GT;
// RetailerSummaryPartial<%@控制语言=C#继承=System.Web.Mvc.ViewUserControl<&IEnumerable的LT; RetailersShipping>>中%GT;< DIV ID =retailer_partial_summary>
<%的foreach(在VAR模型零售商)
{%GT;
< DIV ID =retailer_index_image>
< IMG SRC =<%= Html.En code(retailer.Retailer.Country.Image)%GT;标题=<%= Html.En code(retailer.Retailer.Name)%GT;>中ALT =<%= Html.En code(retailer.Retailer.Name)%GT;类=主图像/>
< BR />
< / DIV>
< DIV ID =retailer_index_logo>
< IMG SRC =<%= Html.En code(retailer.Retailer.Logo)%GT;标题=<%= Html.En code(retailer.Retailer.Name)%GT;>中ALT =<%= Html.En code(retailer.Retailer.Name)%GT;类=主图像/>
< / DIV>
< DIV ID =retailer_index_name_comment>
<%= Html.En code(retailer.Retailer.Name)%GT;< BR />
<跨度><%,如果(retailer.Retailer.CountryId == retailer.Retailer.CountryId)%GT;
<%= Html.En code(retailer.Retailer.LocalComment)%GT;
<%= Html.En code(retailer.Retailer.IntComment)%GT;
< / SPAN>
< / DIV><%}%GT;
< / DIV>
// RetailerCategoryPartial<%@控制语言=C#继承=System.Web.Mvc.ViewUserControl< RetailersCategory>中%GT;
< DIV ID =retailer_index_categories>
<%= Html.En code(Model.Category.CategoryName)%GT;
< / DIV>
我要重构你的视图模型。如果你打算重新$ P $集合这是内psenting集合最适合您的视图模型反映这一点。
公共类RetailersIndexViewModel { 公共IEnumerable的< RetailersShippingViewModel> RetailerShippings {搞定;组; } 公共RetailersIndexViewModel(IEnumerable的< RetailersShipping>运送)
{
this.RetailerShippings =航运;
的foreach(在运送VAR运费)
{
shipping.RetailerCategories = shipping.Categories //假设关键字在你的零售商航运类被引用;
}
}
}公共类RetailerShippingViewModel { 公共IEnumerable的< RetailersCategory> RetailerCategories {搞定;组; } 公共RetailersIndexViewModel(IEnumerable的< RetailersCategory> retailercategories)
{
this.RetailerCategories = retailercategories;
}
}
和使它像这样
<%@页面语言=C#的MasterPageFile =〜/查看/共享/ Inner.Master继承=System.Web.Mvc.ViewPage< RetailersIndexViewModel>中%GT; <%的foreach(在Model.RetailerShippings VAR运费)
{
Html.RenderPartial(RetailerSummaryPartial,发货);
}%GT;
在您的RetailerSummaryPartial称之为而不是索引视图的
<%
的foreach(在ViewData.Model.RetailerCategories VAR类)
{%GT;
<%Html.RenderPartial(RetailerCategoryPartial类); %GT;
<%}%GT;
I have a problem with an MVC view that I just cannot seem to solve. Here it is.
1) I have an index view that displays a list of retailers with data from the retailers table. So far so good.
2) I also want to include the retailer categories for each retailer which are stored in a RetailersCategories table where each retailer can have multiple categories
I have tried a few things but cannot seem to make this work. The closest I came to what I wanted was using a view model. I have included the code below.
I actually get the right data back but I get all of the retailer records and then all of the category records.
What I need is one retailer record at a time with all of the categories that relate to that retailer.
Can anyone show me how I can achieve this?
//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>
I'd refactor your view models. If you're going to be representing a collection within a collection it's best your view models reflect that.
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;
}
}
and render it like so
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Inner.Master" Inherits="System.Web.Mvc.ViewPage<RetailersIndexViewModel>" %>
<% foreach(var shipping in Model.RetailerShippings)
{
Html.RenderPartial("RetailerSummaryPartial", shipping);
}%>
call this in your RetailerSummaryPartial instead of the index view
<%
foreach (var category in ViewData.Model.RetailerCategories)
{%>
<% Html.RenderPartial("RetailerCategoryPartial", category); %>
<% } %>
这篇关于你可以用这个MVC视图模型问题帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!