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 (...) {
 .prop("readonly", true)
 .css('color', "#c0c0c0");

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

Website by WNW Digital