0

I have property Name which I want to be readonly. So inside viewmodel I decorated this property with attribute readonly like

 [ReadOnly(true)]
 public string Name { get; set; }

and inside Edit view I have

@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)

and I'm still able to edit make changes inside textbox.

Should I use some css class to disable this textbox on the view side or this should be enough (if that is the case why this solutions does'nt work).

user1765862
  • 13,635
  • 28
  • 115
  • 220

3 Answers3

1

Have you thought to use @Html.DisplayFor instead of EditorFor which explicitly asks for an editor?

TomTom
  • 61,059
  • 10
  • 88
  • 148
1

Try using editable attubute instead

[Editable(false)]
public string Name { get; set; }

Also see these q&a's

Does ReadOnly(true) work with Html.EditorForModel?

ReadOnly attribute doesn't work in ASP.NET MVC Models

Community
  • 1
  • 1
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
0

Check my answer in Mark a field "Read Only" with Data Annotations

You have to use ReadOnly and Editable based on situation

Community
  • 1
  • 1
Tejasvi Hegde
  • 2,694
  • 28
  • 20