2011-05-08 31 views
1
我沒有實際使用從ASP.NET MVC剃刀

,我使用的是單機版的發現here如何渲染一個Func <Object,TemplateWriter>字符串在Razor視圖引擎Html Helper中?

我已經創建了自己的HtmlHelper描述here

我已經通過試驗確定和錯誤剃鬚刀當在方法的上下文中調用時,屬性會產生一個對象Func<Object, TemplateWriter>

輔助的方法簽名是這樣的:

@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>) 

我怎樣才能得到它作爲一個字符串:

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 string if it has 
    } 

我在我的模板一樣調用它?另外,如果我把它傳遞給另一個模板,如:

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

其中other.cshtml是:

 @Model.Content 

它的工作原理。 Razor.Parse知道我不知道什麼?

謝謝!

回答

1

我假設Object參數是Model。所以,通過查看上面的代碼,我很肯定下面的工作:

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

    //Do Stuff 

    return output 
} 
+0

哇,不能相信我無法弄清楚!謝謝! – 2011-05-08 09:17:58