2

I would like to have the following poco object:

public int Age { get; set; }

[Disabled(Operator.LessThan, "Age", 18)]
public string Country { get; set; }

then on the frontend i would like to generate the following:

Html.TextBoxFor(model => model.Country) 

// and the textbox should be disabled ( get the disabled attr ) if age is // less than 18.

also would be nice to have soimekind of onchange on the age field so that textbox country gets disabled via javascript.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Aflred
  • 4,435
  • 6
  • 30
  • 43
  • 2
    Have you tried writing your own html helper? It shouldn't be too difficult. Alternatively have you had any luck with a display template? – Thomas Boby Feb 18 '16 at 13:13
  • If you want to use an attribute, the you attribute needs to implement IMetadataAware, to add a value to `Metadata.AdditionalValues` and then create a custom `HtmlHelper` extension method (refer [example here](http://stackoverflow.com/questions/26519493/customattribute-reflects-html-attribute-mvc5/26519946#26519946)) for setting the initial `disabled` attribute. Then you would need a script to modify the `disabled` attribute based on selecting the `Age` value in the view. –  Feb 21 '16 at 23:49
  • But disabling a form control means its value will not be posted, causing model binding to fail so it not clear what you trying to achieve with this. –  Feb 21 '16 at 23:49
  • or just put in a form-control 'pointer-events: none, background-color:lightgrey" to fake a disable. – Aflred Feb 22 '16 at 06:22

0 Answers0