【发布时间】:2015-10-16 03:54:47
【问题描述】:
这里是上下文:
这是我的第一个 ASP.NET MVC Web 应用程序。
在类别列表上,我单击一个类别以在新视图中查看有关它的所有信息。
从这个视图中,我显示了它的名称、描述和该类别中的产品数量。
在此之下,我想显示该类别中的所有产品。
我尝试了Html.Action() 方法。
但是当我想遍历foreach 语句时,Visual Studio 会告诉我Type 'System.Web.Mvc.MvcHtmlString' is not enumerable。
这是我的观点(关于类别的详细信息):
@model UlysseCMS.Models.category
@{
ViewBag.Title = "Details about "+ Model.category_name + " category";
var nbProducts = Html.Action("GetNumberOfProductByCategory", "Product", Model.category_id);
var productsList = Html.Action("GetListOfProductsByCategory", "Product", Model.category_id);
}
<div>
<span class="leader">
<span class="mif-chevron-thin-right fg-teal"></span>
<span>@Html.ActionLink("Categories", "Index")</span>
<span class="mif-chevron-thin-right fg-teal"></span>
<span>@Model.category_name</span>
<span class="mif-chevron-right fg-teal"></span>
<span>details</span>
</span>
</div>
<br />
<hr class="bg-teal" />
<br />
<div class="margin30">
<div class="flex-grid">
<div class="row cells7">
<div class="cell colspan3 offset1 sub-header">
Category name :
</div>
<div class="cell colspan4">
@Model.category_name
</div>
</div> <br/>
<div class="row cells7">
<div class="cell colspan3 offset1 sub-header">
Category description :
</div> <br/>
<div class="cell colspan4">
@Model.category_description
</div>
</div> <br/>
<div class="row cells7">
<div class="cell colspan3 offset1 sub-header">
Number of products in this category :
</div>
<div class="cell colspan4">
@nbProducts
</div>
</div>
</div>
</div>
@foreach (var item in productsList)
{
}
这是来自ProductController 的GetListOfProductsByCategory() 方法:
public IEnumerable<product> GetListOfProductsByCategory(int id)
{
return db.product.Where(x => x.product_category_id == id);
}
我仍然继续通过IEnumerable 强制转换或其他方式找到解决方案。
谢谢,
地狱猫。
【问题讨论】:
-
您如何在视图中获取 productsList?
-
html.Action 返回 MvcHtmlString
-
你的动作应该重新运行 ActionResult 我的意思是部分视图
标签: c# asp.net-mvc model-view-controller model ienumerable