2012-04-18 6 views
8
TextBoxFor HtmlHelperのため

を無効にするHTML属性を設定し、私は動的にTextBoxFor HtmlHelperのが動的に

@Html.TextBoxFor(model => model.Street, 
       new 
       { 
        @class = "", 
        disabled = (Model.StageID==(int)MyEnum.Sth) ? "disabled" : "" 
       }) 

ためdisabled属性を設定しようとしているが、disabled=""があったとしても、それはdisabled="disabled"と同じです。これを回避するには?

答えて

19

私は前の月についても同じ問題を抱えていると私はそれ

public static class AttributesExtensions 
{ 
    public static RouteValueDictionary DisabledIf(
     this object htmlAttributes, 
     bool disabled 
    ) 
    { 
     var attributes = new RouteValueDictionary(htmlAttributes); 
     if (disabled) 
     { 
      attributes["disabled"] = "disabled"; 
     } 
     return attributes; 
    } 
} 

その後あなたがこの

@Html.TextBoxFor(
    model => model.Street, 
    new { @class = "" }.DisabledIf(Model.StageID==(int)MyEnum.Sth) 
) 

EDIT(後のようにそれを使用することができ、この拡張メソッドを使用して仕上げPaulcomment):

data-xxxのhtml属性の使用アンダースコアは自動的にマイナス記号に変換されないため、System.Web.Routing.RouteValueDictionaryクラスのコンストラクタを使用してマイニングできます。

代わりにSystem.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributesメソッドを使用してください:この問題を解決します。以下の拡張メソッドを使用し

更新されたコード(拡張メソッド本体のみ)

var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); 
if (disabled) 
{ 
    attributes["disabled"] = "disabled"; 
} 
return attributes; 
+0

それは動作します、私はあなたにビールを借りています!ありがとう – Tony

+0

私はそれを待つことになりますxD –

+0

それはかなり乾いています:>しかし、私はそれが好きです。しかし、私は自分の(より壊れやすい)バリアントを追加しています – goofballLogic

0

は、同様の結果を生じるが、これはおそらくもっと脆弱である:

@Html.TextBoxFor(
    model => model.Street, 
    new { @class = "form-control" } 
).DisabledIf(Model.IsReadOnly) 

エクステンション:

using System.Text.RegularExpressions; 
using System.Web.Mvc; 

namespace xxx.HtmlHelpers 
{ 
    public static class MvcHtmlStringExtensions 
    { 

     private static readonly Regex OpeningTagPattern; 

     static MvcHtmlStringExtensions() 
     { 
      OpeningTagPattern = new Regex("<[a-zA-Z]*"); 
     } 

     public static MvcHtmlString DisabledIf(this MvcHtmlString controlHtml, bool isDisabled) 
     { 
      if (!isDisabled) return controlHtml; 
      return 
       new MvcHtmlString(OpeningTagPattern.Replace(controlHtml.ToString(), 
        x => string.Format("{0} disabled=\"disabled\"", x.Groups[0]))); 
     } 

    } 
} 
+0

上記のChuck Norrisのバージョンを使用するように私のコードを変更しています:) – goofballLogic

0

おそらくあなたのステージIDは、 t

@{ 
    if(Model.StageID != null && Model.StageID > 0) 
    { 
     @Html.TextBoxFor(model => model.Street, 
      new 
      { 
       @class = "", 
       disabled = (Model.StageID==(int)MyEnum.Sth) ? "disabled" : "" 
      }) 
    }else{ 

     @Html.TextBoxFor(model => model.Street, 
      new 
      { 
       @class = "" 
      }) 
    } 
} 
0

実際には同じ問題が発生しました。オーバーロードされたパラメータを持つ拡張メソッドを実装しました。このメソッドは、コントロールを無効にするかどうかを示すブール値を取ります。適切なときには「無効」属性を追加し、組み込みのHtmlHelperで重い作業を処理させます。

拡張クラスとメソッド:その後、あなたは自分の新しいクラスを参照し、それはあなたがTextBoxForオーバーロードのいずれかのこれらの拡張機能を定義する必要があるだろうということは注目に値します@Html.TextBoxFor(m => m.SomeValue, new { @class = "someClass" }, <Your bool value>)

を呼び出す必要が

using System; 
using System.Collections.Generic; 
using System.Linq.Expressions; 
using System.Web.Mvc; 
using System.Web.Mvc.Html; 
using System.Web.Routing; 
public static class OurHtmlHelpers 
{ 
    public const string DisabledAttribute = "disabled"; 

    public static MvcHtmlString TextBoxFor<TModel, TProp>(this HtmlHelper<TModel> htmlHelper, 
                  Expression<Func<TModel, TProp>> expression, 
                  object htmlAttributes, 
                  bool canEdit) 
    { 
     var htmlAttributeDictionary = SetDisabledAttribute(htmlAttributes, canEdit); 

     return htmlHelper.TextBoxFor(expression, htmlAttributeDictionary); 
    }   

    private static RouteValueDictionary SetDisabledAttribute(object htmlAttributes, bool canEdit) 
    { 
     var htmlAttributeDictionary = new RouteValueDictionary(htmlAttributes); 

     if (!canEdit) 
     { 
      htmlAttributeDictionary.Add(DisabledAttribute, DisabledAttribute); 
     } 

     return htmlAttributeDictionary; 
    } 
} 

あなたは使いたいですが、それは妥当なトレードオフのようです。また、機能を追加する他のHtmlHelpersに同じコードの大部分を利用することもできます。

関連する問題