[英]C# ASP.NET MVC 5 - Type 'System.Web.Mvc.MvcHtmlString' is not enumerable
Here is the context : 这是上下文:
This is my first ASP.NET MVC web application. 这是我的第一个ASP.NET MVC Web应用程序。
On the categories list, I click on a category to see all infos about it in a new view. 在类别列表上,我单击一个类别以在新视图中查看有关该类别的所有信息。 From this view, I display its name, its description and the number of products in this category. 从此视图中,我将显示其名称,描述以及此类别中的产品数量。 Below that, I want to display all products in this category. 在此之下,我要显示此类别中的所有产品。 I gave a try to the Html.Action()
method. 我尝试了Html.Action()
方法。 But when I want to iterate through the foreach
statement, Visual Studio tells me that Type 'System.Web.Mvc.MvcHtmlString' is not enumerable
. 但是,当我想遍历foreach
语句时,Visual Studio告诉我Type 'System.Web.Mvc.MvcHtmlString' is not enumerable
。
Here is my view (details about a category): 这是我的观点(有关类别的详细信息):
@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)
{
}
Here is my GetListOfProductsByCategory()
method from the ProductController
: 这是我从ProductController
获得的GetListOfProductsByCategory()
方法:
public IEnumerable<product> GetListOfProductsByCategory(int id)
{
return db.product.Where(x => x.product_category_id == id);
}
I still continue to find a solution with IEnumerable
casting or something. 我仍然继续找到使用IEnumerable
转换之类的解决方案。
Thanks, 谢谢,
Hellcat. 泼妇。
The result of @Html.Action is a View. @ Html.Action的结果是一个View。 When you have an action inside a controller, this action usually return a view, and that rendered view is result of Html.Action ,for displaying it you need to use an @ before html. 当您在控制器中有一个动作时,该动作通常会返回一个视图,并且呈现的视图是Html.Action的结果,要显示该视图,您需要在html之前使用@。 You should write your code as below: 您应该按照以下方式编写代码:
public ActionResult GetListOfProductsByCategory(int id)
{
return View(db.product.Where(x => x.product_category_id == id));
}
Then right click on View and from menu select Add View, then create a partial view. 然后右键单击“视图”,然后从菜单中选择“添加视图”,然后创建局部视图。 inside partial view you can enumerate your list and create the rendered out put. 在局部视图中,您可以枚举列表并创建渲染的输出。 Finally wherever you want to show the rendered list, just call: 最后,只要您想显示渲染列表,只需调用:
@Html.Action("GetListOfProductsByCategory",id)
When creating view, you need to select your model and view type as list so on top of your view you would have: 创建视图时,您需要选择模型和视图类型作为列表,因此在视图顶部您将具有:
@model IEnumerable<product>
Then you must use you foreach as below: 然后,您必须按以下方式使用foreach:
@foreach (var item in Model)
{
}
Okay I modified my Partial View (GetListOfProductsByCategory) like that : 好的,我这样修改了部分视图(GetListOfProductsByCategory):
@model IEnumerable<UlysseCMS.Models.product>
@foreach (var item in Model)
{
<a href="@Url.Action("Details", new { id = item.product_id })">
<div class="tile tile-wide bg-white block-shadow margin30" data-role="tile">
<div class="tile-content slide-up-2">
<div class="slide fg-darkTeal">
<span class="sub-leader" style="padding-left: 5px;">@Html.DisplayFor(modelItem => item.product_name)</span>
</div>
</div>
</div>
</a>
}
And in my Category Details View I display the products like this : 在“类别详细信息”视图中,我将显示以下产品:
@Html.Action("GetListOfProductsByCategory", "Product", Model.category_id)
It's now okay hanks to Mehrdad Babaki's answer. 现在可以感谢Mehrdad Babaki的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.