简体   繁体   中英

How to reuse HTML snippets in asp.net core razor page?

I try to reuse HTML snippets in my razor page (view component) but somehow it never call or hit the break in the html section.

I'm using 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);
            }

What you are trying to do is not (yet) possible. You should take a look at partial views instead:

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

And then inside _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>

somehow it only works with @ though the method call is inside the server side syntax

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM