हम वास्तव में एक ही समस्या में भाग गए। हमने अधिभारित पैरामीटर के साथ एक विस्तार विधि को कार्यान्वित करना समाप्त कर दिया, जो एक बूलियन में लेता है यह दर्शाता है कि हम नियंत्रण अक्षम करना चाहते हैं या नहीं। उचित होने पर हम केवल "अक्षम" विशेषता जोड़ते हैं, और अंतर्निर्मित 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 भार के में से किसी के लिए इन एक्सटेंशन को परिभाषित करना होगा कि लायक है की जरूरत है आप उपयोग करना चाहते हैं, लेकिन यह एक उचित व्यापार बंद की तरह लगता है। आप अन्य एचटीएमएल हेल्पर के लिए उसी कोड का अधिक उपयोग भी कर सकते हैं, जिससे आप कार्यक्षमता जोड़ना चाहते हैं।
यह काम करता है, मैं आप एक बियर देना है! धन्यवाद – Tony
मैं इसके लिए इंतजार करूँगा xD –
जो काफी गालिया है:> लेकिन मुझे यह पसंद है। हालांकि, मैं अपने स्वयं के – goofballLogic