RG-3 RG-3 -4 years ago 145
C# Question

Disabling EditorFor

I have a EditorFor HTML helper like this:

<td>@Html.EditorFor(m => m.Name, belowLevel ? disabledHtmlOptions : null)</td>


object disabledHtmlOptions = new { disabled = "disabled" };

I wanna make this disabled at every time. How do I do that? I dont want to do data annotations because this property is being used in other views too. Only on this view I want to disable it.

Answer Source

if you're stuck on using EditorFor you could set the disabled attribute via jQuery on page load. I know it's not ideal, but it's the only way unless you create an overload for EditorFor that accepts an htmlAttributes collection

$(document).ready(function() {
   $('#Name').attr('disabled', 'disabled');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download