MVC / Razor doesn’t post values from disabled html elements back to controller

In a Razor MVC form, if you disable a html element, the value won’t be posted back to the controller.

@Html.EditorFor(model => model.Person.Surname, new { htmlAttributes = new { @class = "form-control variable" } })

You might do

if (...) {
 $(".variable").attr('disabled', 'disabled'); //doesn't work
 }

Better, do this…

if (...) {
 $(".disable")
 .prop("readonly", true)
 .css('color', "#c0c0c0");
 //https://stackoverflow.com/a/2724483
 }

https://stackoverflow.com/a/2724483

Leave a Reply

Your email address will not be published. Required fields are marked *

Website by Daneswood