1
我想創建一個簡單的HtmlHelper來,我可以用這樣的:創建的HtmlHelper來顯示內容,如果某些條件得到滿足
@using(Html.DisplayIf(Object object))
{
...
}
我試過方法建議here,但不像誰問那傢伙問題我希望括號之間的內容根本不被渲染,而不僅僅是隱藏。
有沒有辦法阻止textwriter寫入括號內的內容,或者其他適合解決我的問題的方法?
我想創建一個簡單的HtmlHelper來,我可以用這樣的:創建的HtmlHelper來顯示內容,如果某些條件得到滿足
@using(Html.DisplayIf(Object object))
{
...
}
我試過方法建議here,但不像誰問那傢伙問題我希望括號之間的內容根本不被渲染,而不僅僅是隱藏。
有沒有辦法阻止textwriter寫入括號內的內容,或者其他適合解決我的問題的方法?
編輯
您可以使用下面介紹的方法:Capture wrapped content in BeginForm style disposable html helper。 我已將第一種方法應用於您的示例。
public static class HtmlExtensions
{
public static HelperResult DisplayIf(this HtmlHelper html, Func<object, HelperResult> template, bool show)
{
return new HelperResult(writer =>
{
if (show)
{
template(null).WriteTo(writer);
}
});
}
}
你可以這樣調用:
@* Will render *@
@Html.DisplayIf(
@<span>test1</span>, true
)
@* Will not render *@
@Html.DisplayIf(
@<span>test2</span>, false
)
我不工作,內容還是寫了兩對的TextWriter – severin 2014-09-01 14:16:36
什麼是對象參數包含哪些內容?輸出內容需要進行什麼樣的檢查? – 2014-09-01 14:23:19
怎麼樣DisplayIf(這個HtmlHelper htmlHelper,布爾秀)'? – 2014-09-01 14:38:43