2012-04-18 6 views
8

के लिए विकलांग एचटीएमएल विशेषता निर्धारित मैं गतिशील TextBoxFor HtmlHelperगतिशील रूप 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) 
) 

संपादित की तरह उपयोग कर सकते हैं के लिए (Paul का comment):

data-xxx एचटीएमएल विशेषताओं का उपयोग 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

मैं ऊपर कोड चक नॉरिस संस्करण का उपयोग करने के लिए अपना कोड बदल रहा हूँ :) – goofballLogic

0

शायद आपका मंच आईडी नहीं मिल रहा है टी

@{ 
    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 को भारी उठाने को संभालने दें।

एक्सटेंशन वर्ग और विधि:

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; 
    } 
} 

तो फिर तुम बस अपने नए वर्ग का संदर्भ और फोन @Html.TextBoxFor(m => m.SomeValue, new { @class = "someClass" }, <Your bool value>)

यह ध्यान देने योग्य आप TextBoxFor भार के में से किसी के लिए इन एक्सटेंशन को परिभाषित करना होगा कि लायक है की जरूरत है आप उपयोग करना चाहते हैं, लेकिन यह एक उचित व्यापार बंद की तरह लगता है। आप अन्य एचटीएमएल हेल्पर के लिए उसी कोड का अधिक उपयोग भी कर सकते हैं, जिससे आप कार्यक्षमता जोड़ना चाहते हैं।

संबंधित मुद्दे