Issue: Razor TextBoxFor returns null in Controller

Explanation:

I had a couple of textbox’s using razor syntax and some had an html attribute disabled in order to stop the user from editing. On Postback, the disabled textboxes kept on returning null. I made a couple of tests. At first I thought it is because I am setting the value in jQuery which was not the case. The problem is that the form ignors all disabled fields, even if they contain data. So the trick was to simply removed disabled and replace it with readonly.

Example:

Wrong:

@Html.TextBoxFor(model => model.username, new { @disabled= “disabled” })

Correct:

@Html.TextBoxFor(model => model.username, new { @readonly = “readonly” })