2012-04-18 13 views

Odpowiedz

19

Mam ten sam problem o miesiąc temu skończyłem przy użyciu tej metody rozszerzenie na to

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

I po tym można go używać jak to

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

EDIT (po Paul 's comment):

Korzystanie z atrybutów html w wersji data-xxx może być wydobywany za pomocą konstruktora klasy System.Web.Routing.RouteValueDictionary, ponieważ podkreślenia nie będą automatycznie konwertowane na znak minus.

Zamiast tego skorzystaj z metody System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes: rozwiąże to problem.

zaktualizowany kod (Extension metoda tylko korpus)

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

to działa, jestem winien Ci piwo! Dzięki – Tony

+0

będę czekać na to xD –

+0

, który jest dość bezczelny:> ale lubię to. Dodaję jednak (bardziej delikatny) wariant mojego własnego. – goofballLogic

0

Stosując metodę rozszerzenia poniżej daje podobne wyniki, ale jest to chyba bardziej kruche:

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

Rozszerzenie:

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

Zamieniam swój kod, żeby użyć wersji Chucka Norrisa powyżej :) – goofballLogic

0

Prawdopodobnie twój etap Id nie jest se se 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

Po prostu wpadliśmy na ten sam problem. W końcu wdrożyliśmy metodę rozszerzenia z przeciążonymi parametrami, która przyjmuje wartość logiczną wskazującą, czy chcemy wyłączyć kontrolę. Dodajemy po prostu atrybut "disabled" i pozwalamy wbudowanemu HtmlHelperowi na podnoszenie ciężarów.

klasa Rozbudowa i metoda:

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

Potem wystarczy odwołać swoją nową klasę i wywołać @Html.TextBoxFor(m => m.SomeValue, new { @class = "someClass" }, <Your bool value>)

Warto zauważyć, że trzeba by określić te rozszerzenia do jakichkolwiek przeciążeń TextBoxFor chciałbyś użyć, ale wydaje się, że rozsądny kompromis. Możesz również wykorzystać większość tego samego kodu dla innych HtmlHelperów, do których chcesz dodać funkcję.

Powiązane problemy