Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a readonly text using html helper in asp.net mvc?

Tags:

asp.net-mvc

I want to disable a textbox in the view. So I use following code:

<%= Html.TextBox("ID", Model.ID, new { readonly="true" })%> 

or

<%= Html.TextBox("ID", Model.ID, new { enable="false" })%> 

Both of them don't work. what's the solution?

like image 793
KentZhou Avatar asked Jul 20 '09 14:07

KentZhou


People also ask

How do you add a readonly in HTML helper?

TextBoxFor(m => m.ID, new { @readonly = "readonly" }) it works as expected.

How set readonly property of TextBox in MVC?

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 you make an editor read only in MVC?

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


2 Answers

Try

<%= Html.TextBox("ID", Model.ID, new { @readonly="readonly" })%> 

I'm not sure you have to use the overload with 4 parameters. You should be able to use the one with 3, but you need to append @ to the readonly since readonly is a keyword in C#. And setting @readonly to readonly is XHTML compliant.

like image 182
Brandon Avatar answered Oct 03 '22 07:10

Brandon


Try

<%= Html.TextBox("ID", Model.ID, null, new { @readonly="true" })%> 

instead of

<%= Html.TextBox("ID", Model.ID, new { @readonly="true" })%> 

If you check the documentation, you can see that the third parameter is not htmlAttributes, as you probably expected.

You need to use the overload with four parameters.

like image 23
mookid8000 Avatar answered Oct 03 '22 08:10

mookid8000