开发者

How to use nested inline templates in ASP.NET MVC Razor?

开发者 https://www.devze.com 2023-02-16 18:13 出处:网络
Is there a way to nest templates in the Razor view-engine such like this in the older Asp.net viewengine? For as much as i am aware Razor only supports one level of templates.

Is there a way to nest templates in the Razor view-engine such like this in the older Asp.net viewengine? For as much as i am aware Razor only supports one level of templates.

-John

   <%Html.Do(new string[]{"Chris"开发者_开发知识库,"John"}, name =>
    { 
        %>

        <li><%=name%></li>

        <%Html.Do(new string[]{"Beer","Fries"}, stuff => { 
            %>
            <li><%=stuff%></li>
            <% 
        }); %>

        <hr size=1 />

        <% 
    }
); %>


Change your function to take a Func<T, HelperResult>, then pass @<li>@item</li>

EDIT: For example:

public static IHtmlString Do<T>(this HtmlHelper html, IEnumerable<T> items, Func<T, HelperResult> template) {
    return html.Raw(String.Join("\n", items.Select(o => template(o).ToString())));
}

In Razor:

@Html.Do(new []{"Beer","Fries"}, @<li>@item</li>)
0

精彩评论

暂无评论...
验证码 换一张
取 消