發表@epignosisx工作的解決方案,但如果你想添加一些其他的屬性,因爲你必須將它添加它的兩個對象(其中帶有disabled
它可能是一個問題,現在它是空的)。
更糟糕的是,如果你有其他的布爾屬性,因爲你將有四個不同的對象,每一個對於每個組合。
這裏最好的解決方案(多一點代碼)是爲HtmlHelper構建一個擴展方法來接收布爾屬性作爲參數。
public static MvcHtmlString TextBoxDisabledFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool disabled, object htmlAttributes = null)
{
return TextBoxDisabledFor(htmlHelper, expression, disabled, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString TextBoxDisabledFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool disabled, IDictionary<string, object> htmlAttributes)
{
if (htmlAttributes == null)
htmlAttributes = new Dictionary<string, object>();
if (disabled)
htmlAttributes["disabled"] = "disabled";
return htmlHelper.TextBoxFor(expression, htmlAttributes);
}
Here there is another example
@epig ....有趣。 ViewBag.Disabled的價值是什麼? – MikeTWebb 2012-01-31 20:12:40
@MikeTWebb ViewBag.Disabled應該是一個布爾值。 – epignosisx 2012-01-31 20:15:32
@epig .... true或false是值。很棒!謝謝 – MikeTWebb 2012-01-31 20:16:13