C# ASP.NET MVC 5 - 类型 'System.Web.Mvc.MvcHtmlString' 不可枚举



这是上下文:
这是我的第一个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)
{
}

我的GetListOfProductsByCategory()方法来自ProductController:
public IEnumerable<product> GetListOfProductsByCategory(int id)
{
    return db.product.Where(x => x.product_category_id == id);
}

我仍然继续寻找IEnumerable铸造或其他解决方案。

谢谢,

悍妇。

@Html的结果。动作是一个视图。当你在控制器中有一个动作时,这个动作通常会返回一个视图,而呈现的视图是Html的结果。操作,为了显示它,您需要在html之前使用@。你应该这样写代码:

public ActionResult GetListOfProductsByCategory(int id)
{
    return View(db.product.Where(x => x.product_category_id == id));
}

然后右键单击"视图",从菜单中选择"添加视图",然后创建部分视图。在局部视图中,您可以枚举列表并创建渲染输出。最后,无论你想在哪里显示渲染列表,只需调用:

@Html.Action("GetListOfProductsByCategory",id)

当创建视图时,你需要选择你的模型和视图类型作为列表,这样在视图的顶部你会有:

@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的回答,现在一切都好了。

相关内容

最新更新