开发者

Howto render a Func<Object, TemplateWriter> to string in Razor view engine Html Helper?

I am not actually using the Razor from ASP.NET MVC, I am using the standalone version as found here

I have created my own HtmlHelper as described here

I have determined via trial and error that razor <text> attributes produce a Func<Object, TemplateWriter> object when called in the context of a method.

The method signature of the helper looks like:

    public String IncludeOnce(Func<Object, TemplateWriter> text) {
       //here I need to be able to render the text Func to a string so
       //I can do some checks, and return it if it hasnt yet been included
       //or return an empty str开发者_开发技巧ing if it has
    }

I am invoking it in my template like:

    @Html.IncludeOnce(
    @<text>
      <style type="text/css">
        /* styles I only want on the page once, and not everytime the template
           is rendered. Note: I need @Model to work here too*/
        .something { top: @Model.Top }
      </style>
    </text>)

How can I get it as a String? Also, If I pass it to another template, eg:

      public String IncludeOnce(Func<Object, TemplateWriter> text) {
          return Razor.Parse("other.cshtml", new { Content = text(new Object()) })
      }

where other.cshtml is:

      @Model.Content

it works. What does Razor.Parse know that I dont?

Thanks!


I'm assuming the Object parameter is the Model. So, by looking at your code above, I'm pretty sure the following will work:

public String IncludeOnce(Func<Object, TemplateWriter> text) {
   string output = text(Model).ToString();

   //Do Stuff

   return output 
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜