MVC3 Read Only Text Box but value must be POSTed

asp.net-mvc-3

Is there a way in MVC3 to make a read-only Text Box where the value is POST'ed back?

I have tried the following (from others on this site):

@Html.TextBoxFor(m => m.Model.xxxx, new { disabled = "disabled", @class = "text-box single-line", @readonly = "readonly" }) 

However the value does not get POST'ed, inspecting it gives NULL.
I used a text box for styling reasons and because I thought it would post back correctly. I have JScript on the page that updates the value, hence why Its important to be able to get back its POST'ed value.

I will start looking at using a DisplayFor and HiddenFor pair but would like to know if there's a more elegant solution.

Best Answer

The Html specification says that disabled fields should not be submitted therefore you can only use readonly in your situation, where you want to update the value from JS and submit it. readonly with a CSS class that makes it look disabled gives a better UI experience. Alternatively, just use a span plus hidden input.

See: http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1

"Disabled controls cannot be successful." and "A successful control is "valid" for submission."

Related Topic