开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜