ASP.Net MVC3 - Pass razor markup as a parameter

后端 未结 4 1607
予麋鹿
予麋鹿 2020-12-30 00:38

I have a helper called EditableArea which provides a user with a runtime-editable div (via JS). EditableArea helper checks if an editable area (not

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 01:42

    Here's an example I use to render jQuery Template markup by passing in a template Id and razor-style syntax for the template itself:

    public static MvcHtmlString jQueryTmpl(this HtmlHelper htmlHelper, 
        string templateId, Func template) 
    {
        return MvcHtmlString.Create("");
    }
    

    and this would be called with

    @Html.jQueryTmpl("templateId", @any type of valid razor syntax here)
    

    Basically just use Func as your parameter and template.Invoke(null) (with arguments if necessary) to render it. Obviously you can skip the call to .Invoke() to avoid rendering the "default" markup.

提交回复
热议问题