Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the @Html.EditorFor Disabled

I have the following inside my asp.net mvc web application :-

<div><span class="f">Data Center Name</span> @Html.EditorFor(model => model.Switch.TMSRack.DataCenter.Name, new  { disabled = "disabled" })</div> 

but the field will not be disabled ,, can anyone adivce please? THanks

like image 264
john Gu Avatar asked Oct 31 '13 10:10

john Gu


People also ask

How do I make EditorFor readonly?

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

What is HTML EditorFor?

The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property. The following table list the data types and releted HTML elements: DataType.

What is the difference between TextBoxFor and EditorFor?

TextBoxFor will always show the textbox element no matter which kind of property we are binding it with. But EditorFor is much flexible in this case as it decides which element will be more suitable to show the property.


2 Answers

@Html.EditorFor() does not have an overload to support htmlAttributes. You could try @Html.TextBoxFor()

@Html.TextBoxFor(model => model.propertyName, new {disabled= "disabled" }) 

If you are using system key words such as class in htmlAttributes please add @ before the attribute name.

Ex:

@Html.TextBoxFor(model => model.propertyName, new {@class = "disabledClass" }) 
like image 98
Kaf Avatar answered Sep 18 '22 15:09

Kaf


Using MVC 5, @Html.EditorFor() supports htmlAttributes

@Html.EditorFor(model => model.x, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } }) 

The above example shows how you can add a class and also the disabled attribute

like image 26
SaadK Avatar answered Sep 20 '22 15:09

SaadK