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.

4
  • 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? Commented Feb 18, 2016 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) 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. Commented Feb 21, 2016 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. Commented Feb 21, 2016 at 23:49
  • or just put in a form-control 'pointer-events: none, background-color:lightgrey" to fake a disable. Commented Feb 22, 2016 at 6:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.