2016-04-10 5 views
0

FlagAccessEdit = falseの場合、無効化または読み取り専用にするにはどうすればよいですか?

動的オブジェクトに属性を追加するにはどうすればよいですか?

public static MvcHtmlString CCheckBox(this HtmlHelper htmlHelper, 
string name,object htmlAttributes, 
bool FlagAccessEdit = true, bool FlagAccessView = true) 
{ 

      if (!FlagAccessView) 
       return MvcHtmlString.Empty; 
      else if (!FlagAccessEdit && FlagAccessView) 
      { 
       return htmlHelper.CheckBox(name, htmlAttributes); 
      } 
      else 
       return htmlHelper.CheckBox(name, htmlAttributes); 
} 
+0

がどのように= falseを無効にするか、読み取り専用のID FlagAccessEditを作成するには、以下のような方法

のヘルパーメソッドですか? –

+0

このコメントが質問の一部である場合は、コメントではなく質問に編集する必要があります。 – petric

+0

Ashish Shuklaの回答が1つの解決策を示していますが、その点は何でしょうか。読み取り専用のチェックボックスはありません。無効にすると、最初にチェックボックスがチェックされていても(false)、常にfalseを返すことになります。あなたがこれをしたい場合、値のいくつかのテキスト( "はい"または "いいえ"と言う)の隠し入力を生成 –

答えて

0

既存htmlAttributeを取得し、無効を追加したり、読み取り専用、あなたの条件に基づいてする必要があります。以下は、正しいコードは

public static MvcHtmlString CCheckBox(this HtmlHelper htmlHelper, 
     string name, object htmlAttributes,bool FlagAccessEdit = true, 
     bool FlagAccessView = true) 
    { 
     //get the htmlAttribute 
     IDictionary<string, object> attributes = new RouteValueDictionary(htmlAttributes); 

     if (!FlagAccessView) 
      return MvcHtmlString.Empty; 
     else if (!FlagAccessEdit && FlagAccessView) 
     { 
      //Add the disabled attribute 
      attributes.Add("disabled", "disabled"); 
      return htmlHelper.CheckBox(name, attributes); 
     } 
     else 
     { 
      return htmlHelper.CheckBox(name, htmlAttributes); 
     } 

    } 

コール

@Html.CCheckBox("chkCheckbox", new { id="chkDemo"},false,true) 
+0

あまりAshish。ありがとうございました。 –

関連する問題