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
EditorFor(model => model. userName, new { htmlAttributes = new { disabled = "disabled", @readonly = "readonly" } }) .
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.
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.
@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" })
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With