如何在asp.net核心剃须刀页面中重用HTML代码段?

时间:2019-03-19 10:21:59

标签: c# asp.net-core razor

我尝试在我的剃须刀页面(查看组件)中重用HTML代码段,但是以某种方式它从未调用或击中html部分的中断。

我正在使用Asp.net core 2.2

        @{
        Func<CategorySimpleModel, Microsoft.AspNetCore.Html.IHtmlContent> DisplayManufacturerPicture=
            @<div class="col-sm-4">
                <div class="row">
                    @foreach (var m in item.Manufacturers)
                    {
                        <div class="col-md-6 col-sm-12">
                            blah blah
                        </div>
                    }
                </div>
            </div>;
        }

   @foreach (var root in Model.Categories)
            { 
               DisplayManufacturerPicture (root);
            }

2 个答案:

答案 0 :(得分:0)

您尚未尝试做的事情。您应该改为查看partial views

@foreach (var root in Model.Categories)
{
    <partial name="_DisplayManufacturerPicture" model="root" />
}

然后在_DisplayManufacturerPicture.cshtml内:

@model CategorySimpleModel

<div class="col-sm-4">
    <div class="row">
        @foreach (var m in item.Manufacturers)
        {
            <div class="col-md-6 col-sm-12">
                blah blah
            </div>
        }
    </div>
</div>

答案 1 :(得分:0)

尽管方法调用在服务器端语法内部,但它只能以@方式工作

 @if (root.Manufacturers.Count > 0)
{
   @DisplayManufacturerPicture(root);
}