Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 EditorFor readOnly

I want to make readOnly with EditorFor in edit page.

I tried to put readonly and disabled as:

<div class="editor-field">         @Html.EditorFor(model => model.userName, new { disabled = "disabled", @readonly = "readonly" })     </div> 

However, it does not work. How can I make to disable edit this field?

Thank you.

like image 276
wholee1 Avatar asked Apr 11 '12 15:04

wholee1


1 Answers

The EditorFor html helper does not have overloads that take HTML attributes. In this case, you need to use something more specific like TextBoxFor:

<div class="editor-field">     @Html.TextBoxFor(model => model.userName, new          { disabled = "disabled", @readonly = "readonly" }) </div> 

You can still use EditorFor, but you will need to have a TextBoxFor in a custom EditorTemplate:

public class MyModel {     [UIHint("userName")]     public string userName { ;get; set; } } 

Then, in your Views/Shared/EditorTemplates folder, create a file userName.cshtml. In that file, put this:

@model string @Html.TextBoxFor(m => m, new { disabled = "disabled", @readonly = "readonly" }) 
like image 142
danludwig Avatar answered Oct 21 '22 07:10

danludwig