Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is readonly for in a disabled TextBoxFor?

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

like image 961
Felipe Athayde Avatar asked Feb 26 '14 20:02

Felipe Athayde


People also ask

How do you make a TextBoxFor readonly?

The TextBoxes can be made ReadOnly by setting the HTML ReadOnly attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions.

How do I disable TextBoxFor in HTML?

You can make the input 'read only' by using 'readonly'. This will let the data be POSTED back, but the user cannot edit the information in the traditional fashion. Keep in mind that people can use a tool like Firebug or Dragonfly to edit the data and post it back.

How do you make an editor read only in MVC?

EditorFor(model => model. userName, new { htmlAttributes = new { disabled = "disabled", @readonly = "readonly" } }) .


1 Answers

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 = "" }) 
like image 123
Brandon Avatar answered Oct 12 '22 10:10

Brandon