2012-01-31 94 views
26

我有一個C#.Net網絡應用程序。在那個應用程序中,我需要根據誰登錄系統,有條件地禁用Html.TextBoxFor控件(也可以是Html.DropDownListFor控件)。我試着用MVC3有條件地禁用Html.TextBoxFor()

@Html.TextBoxFor(model => model.ProposalName, new { @ViewBag.IsDisabled }) 

@ViewBag.IsDisabled被設置爲的String.Empty「禁用」在控制器中。但是,這會呈現爲IsDisabled='disabled'IsDisabled="",因此該控件未禁用。當我嘗試

@Html.TextBoxFor(model => model.ProposalName, new { @ViewBag.Disabled }) 

即使ViewBag.Disabled不包含任何文本,控件始終被禁用。我如何有條件地禁用Html.TextBoxFor()控件?

回答

51

嘗試

@Html.TextBoxFor(model => model.ProposalName, ViewBag.Disabled ? (object)new { disabled="disabled" } : new {}) 
+0

@epig ....有趣。 ViewBag.Disabled的價值是什麼? – MikeTWebb 2012-01-31 20:12:40

+1

@MikeTWebb ViewBag.Disabled應該是一個布爾值。 – epignosisx 2012-01-31 20:15:32

+0

@epig .... true或false是值。很棒!謝謝 – MikeTWebb 2012-01-31 20:16:13

19

發表@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

8

我有同樣的問題,並決定寫我自己的HtmlHelper擴展方法。

public static MvcHtmlString Disable(this MvcHtmlString helper, bool disabled) 
    { 
     if (helper == null) 
      throw new ArgumentNullException(); 

     if (disabled) 
     { 
      string html = helper.ToString(); 
      int startIndex = html.IndexOf('>'); 

      html = html.Insert(startIndex, " disabled=\"disabled\""); 
      return MvcHtmlString.Create(html); 
     } 

     return helper; 
    } 

這將接受一個布爾值來指示控件是否應該被禁用。它只是在第一個>內附加disabled="disabled",它在字符串中出現。

您可以像下面一樣使用它。

@Html.TextBoxFor(model => model.ProposalName).Disable(true)

+0

很有用...... – Bohn 2016-08-02 19:36:20

5

這裏是我使用的方法,它不需要擴展,並且不限制你只有一個HTML屬性。它假定存在一個名爲模型中的「禁用」布爾屬性,但因爲它的計算結果爲Boolean對於三元運算符,你可以把你想要什麼在那裏,只要:

@Html.TextBoxFor(model => model.Whatever, 
    new Dictionary<string, object>() { 
    { "size", "5" }, 
    { "class", "someclasshere" }, 
    { model.Disabled ? "disabled" : "data-notdisabled", "disabled" } 
    }) 

與標準快捷的限制表示法是該屬性的名稱不能是動態的。通過創建正確類型的字典,可以使屬性名稱爲動態,然後將該字典作爲屬性字典傳遞給文本框。當該字段不被禁用時,它將傳遞一個名爲「data-notdisabled」的屬性,而不是一個名爲「disabled」的屬性,瀏覽器將忽略該屬性。

2

擴展@詹姆斯的回答,我寫這個的HtmlHelper擴展,更新/刪除disabled屬性,如果它已經存在,或將其添加如果不是:

public static MvcHtmlString Disable(this MvcHtmlString helper, bool disabled) { 
    string html = helper.ToString(); 
    var regex = new Regex("(disabled(?:=\".*\")?)"); 
    if (regex.IsMatch(html)) { 
     html = regex.Replace(html, disabled ? "disabled=\"disabled\"" : "", 1); 
    } else { 
     regex = new Regex(@"(\/?>)"); 
     html = regex.Replace(html, disabled ? "disabled=\"disabled\"$1" : "$1", 1); 
    } 
    return MvcHtmlString.Create(html); 
} 

它也自動關門標籤很好地發揮(如<input />)。

用法是相同的:

@Html.TextBoxFor(model => model.PropertyName).Disable(true) 

測試兩個@Html.DropDownListFor()@Html.TextBoxFor()