What is readonly for in a disabled TextBoxFor?

Felipe Athayde picture Felipe Athayde · Feb 26, 2014 · Viewed 58.1k times · Source

I have two different ways to use a DISABLED TextBoxFor, which is:

@Html.TextBoxFor(u => u.Visibilidade, new { disabled = "disabled", @readonly = "readonly" })

and

@Html.TextBoxFor(u => u.Visibilidade, new { disabled = "disabled" })

ie. using or not readonly property

What is the difference, considering that a disabled field will not be changed any way?

Thanks in advance

Answer

Brandon picture Brandon · Feb 26, 2014

Usually you would use one or the other, not both.

Readonly allows users to focus on the textbox to copy text or trigger an event. Readonly fields will be posted with the form.

With a disabled field, users cannot give focus to the textbox and the field will NOT be posted with the form.

Which one you use depends on what you need to do with the field.

If you want to enable focus but don't want it posted, you can make it readonly, but override the name property.

@Html.TextBoxFor(u => u.Visibilidade, new { @readonly = "readonly", @Name = "" })