3

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.

caesay
  • 16,932
  • 15
  • 95
  • 160
RG-3
  • 6,088
  • 19
  • 69
  • 125
  • raj, you risk not getting a great answer here as your description of the problem is a little vague. is there any way to expand on the problem and required solution, perhaps with your example being expanded a little plus the background subtext if possible – jim tollan Jan 27 '12 at 16:02
  • @Jim, I dont know how can I describe it more correctly, however, I only need to disable my textbox or editorfor box where Name is being displayed. I can display in Label but thats not the requirement. – RG-3 Jan 27 '12 at 16:05
  • raj - i think your edit just helped :) – jim tollan Jan 27 '12 at 16:22
  • By the way, be careful with this, because even though the input is disabled your users can still send you a new value. – Draganov Jan 28 '12 at 10:00

4 Answers4

8

MVC 5.1 now allows passing in HTML attributes in EditorFor (see this answer). So you could do this:

@Html.EditorFor(model => m.Name, new { htmlAttributes = new { disabled = "disabled" } })
Community
  • 1
  • 1
Peeter Kokk
  • 1,625
  • 2
  • 15
  • 9
4

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');
});
Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
3

You could just render HTML for the view. If it's meant to be "read-only" just render the text. Otherwise you could render an <input> element.

For example, instead of

<td>@Html.EditorFor( m => m.Name )</td>

do

<td>@Model.Name</td>

or

<td><input type="text">@Model.Name</input></td>
contactmatt
  • 18,116
  • 40
  • 128
  • 186
  • 1
    m does not exist in the current context when I tried this @m.Name ? – RG-3 Jan 27 '12 at 16:03
  • try @Model.Name, (beat me to it @Jisaak). Updated answer. – contactmatt Jan 27 '12 at 16:05
  • I updated my question. I have an object where I am disabling it. Still it doesnt work. – RG-3 Jan 27 '12 at 16:13
  • 1
    @Raj, if you need it disabled, won't my first option work? Rendering it as text (@Model.Name) will make it uneditable. If you need a disabled input box, just put disabled="disabled" in the input element itself. If you need to conditionally add the disabled attribute, you can do that also. – contactmatt Jan 27 '12 at 16:16
  • @contactmatt: So you are saying something like this: @Html.EditorFor(m => m.Name, disabled = "disabled" ) – RG-3 Jan 27 '12 at 16:25
  • @Raj, I don't think that will work...have you tried doing @Model.Name ? – contactmatt Jan 27 '12 at 16:58
  • @Model.Name will show me something like label form. I dont want it. I want a disabled textbox UI. – RG-3 Jan 27 '12 at 18:07
3

I don't believe the signature of the EditorFor method allows you to specify any HTML attributes. You can if you change it to TextBoxFor however.

<td>@Html.TextBoxFor(m => m.Name, belowLevel ? disabledHtmlOptions : null)</td>
Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101