C# ASP.NET MVC 5 - 类型 'System.Web.Mvc.MvcHtmlString' 不可枚举
C# ASP.NET MVC 5 - Type 'System.Web.Mvc.MvcHtmlString' is not enumerable
这里是上下文:
这是我的第一个 ASP.NET MVC 网络应用程序。
在类别列表中,我单击一个类别以在新视图中查看有关它的所有信息。
从这个视图中,我显示了它的名称、描述和该类别中的产品数量。
在此之下,我想显示该类别中的所有产品。
我尝试了 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)
{
}
这是我的 GetListOfProductsByCategory()
来自 ProductController
的方法:
public IEnumerable<product> GetListOfProductsByCategory(int id)
{
return db.product.Where(x => x.product_category_id == id);
}
我仍在继续寻找 IEnumerable
铸造之类的解决方案。
谢谢,
地狱猫。
@Html.Action 的结果是一个视图。
当你在控制器中有一个动作时,这个动作通常是 return 一个视图,渲染的视图是 Html.Action 的结果,为了显示它你需要在 html 之前使用 @。
您应该编写如下代码:
public ActionResult GetListOfProductsByCategory(int id)
{
return View(db.product.Where(x => x.product_category_id == id));
}
然后右键单击视图,然后从菜单 select 添加视图,然后创建局部视图。
在部分视图中,您可以枚举列表并创建渲染输出。
最后,无论你想在哪里显示渲染列表,只需调用:
@Html.Action("GetListOfProductsByCategory",id)
创建视图时,您需要select您的模型和视图类型为列表,因此在您的视图顶部您将拥有:
@model IEnumerable<product>
那么你必须如下使用foreach:
@foreach (var item in Model)
{
}
好的,我修改了我的局部视图 (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>
}
在我的类别详细信息视图中,我显示这样的产品:
@Html.Action("GetListOfProductsByCategory", "Product", Model.category_id)
感谢 Mehrdad Babaki 的回答,现在可以了。
这里是上下文:
这是我的第一个 ASP.NET MVC 网络应用程序。
在类别列表中,我单击一个类别以在新视图中查看有关它的所有信息。
从这个视图中,我显示了它的名称、描述和该类别中的产品数量。
在此之下,我想显示该类别中的所有产品。
我尝试了 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)
{
}
这是我的 GetListOfProductsByCategory()
来自 ProductController
的方法:
public IEnumerable<product> GetListOfProductsByCategory(int id)
{
return db.product.Where(x => x.product_category_id == id);
}
我仍在继续寻找 IEnumerable
铸造之类的解决方案。
谢谢,
地狱猫。
@Html.Action 的结果是一个视图。 当你在控制器中有一个动作时,这个动作通常是 return 一个视图,渲染的视图是 Html.Action 的结果,为了显示它你需要在 html 之前使用 @。 您应该编写如下代码:
public ActionResult GetListOfProductsByCategory(int id)
{
return View(db.product.Where(x => x.product_category_id == id));
}
然后右键单击视图,然后从菜单 select 添加视图,然后创建局部视图。 在部分视图中,您可以枚举列表并创建渲染输出。 最后,无论你想在哪里显示渲染列表,只需调用:
@Html.Action("GetListOfProductsByCategory",id)
创建视图时,您需要select您的模型和视图类型为列表,因此在您的视图顶部您将拥有:
@model IEnumerable<product>
那么你必须如下使用foreach:
@foreach (var item in Model)
{
}
好的,我修改了我的局部视图 (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>
}
在我的类别详细信息视图中,我显示这样的产品:
@Html.Action("GetListOfProductsByCategory", "Product", Model.category_id)
感谢 Mehrdad Babaki 的回答,现在可以了。