Asp.net-Mvc-3
剃刀替代 Html.Raw()
我在一個方法中列印出 html。這是程式碼:
@Html.Raw(Html.GenerateTabs()) //works -- but is inconvinent實在是不想每一個方法都這樣。這是我想做的方式。但它不起作用。當我在瀏覽器中執行程式碼 html 時。
@Html.GenerateTabs() //prints html in the broswer <text>@Html.GenerateTabs()</text> //prints html in the broswer有沒有一種剃須刀友好的方式來做到這一點?
如果您的程式碼輸出 MvcHtmlString 而不是字元串,它將正確輸出其中包含的 HTML。
您可以從帶有 HTML 的字元串中使用它所具有的靜態工廠方法構造 MvcHtmlString。
public MvcHtmlString OutputHtml() { return MvcHtmlString.Create("<div>My div</div>"); }然後在視圖中:
@OutputHtml()
Razor 預設編碼。到目前為止,我還沒有發現在視圖級別或應用程序級別將其關閉。
也許做一個擴展?
public static MvcHtmlString RawHtml(this string original) { return MvcHtmlString.Create(original); }…
@Html.GenerateTabs().RawHtml();