16

I have marked a property as readonly in the model class, like this:

public class RegisterModel
{
    [Display(Name = "User name")]
    [ReadOnly(true)]
    public string UserName { get; set; }
    ...
}

and in my view:

@Html.EditorFor(m => m.UserName)

but when I run the application, the textbox is not readonly.

I know I can use html attributes in the view to make it readonly, but I would prefer if this can be done in the model class itself.

Can it be achieved?

Edi Wang
  • 3,547
  • 6
  • 33
  • 51

4 Answers4

15

[Update] I don't think it is possible without new { @readonly = "readonly" }.Readonly property specifies whether the property this attribute is bound to is read-only or read/write. Details Here.

But you could try for Custom Helpers or try Using Editable instead Readonly on the model and use the metadata property in your View.

[Editable(false)]

I guess you have already looked into Does ReadOnly(true) work with Html.EditorForModel?

also a fine article odetocode.com

Community
  • 1
  • 1
Shubh
  • 6,693
  • 9
  • 48
  • 83
  • Another excellent point. But more importantly, don't count on annotations for everything. They're great, but we get spoiled and expect them to wipe after us a little too often – Dave Alperovich Feb 21 '13 at 06:16
  • 6
    Baffled as to how this is the selected answer. I set up a trivial sample project and this solution does not work. The answer by Victor below is the only solution. I understand he wants to specify this in the model, but it can't be done that way. – CoderSteve Nov 05 '14 at 13:20
  • I'd like to point out that while setting it readonly in the html will prevent a general user from editing the text box, anyone with a little knowledge in html and javascript can mess everything up so do not rely on html attributes to protect your data. The goal is to prevent the model from being changed regardless of what happened on the front end. – Richard Barker Oct 27 '16 at 19:52
11

ReadOnly attribute doesn't block the HMTL helper to display the field as an enabled input. It is an information that the only the MVC data Binder will respect.

It means that RegisterModel instance, which will be posted back after form submission by a user, will always has null value on it's UserName property, regardless of user's input in the appropriate field of the form.

Deilan
  • 4,740
  • 3
  • 39
  • 52
Rabskatran
  • 2,269
  • 5
  • 27
  • 41
9

ReadOnly attribute does not set the input to read-only.

Try this

Html.TextBoxFor(x => x.UserName, new { readonly = "readonly" })
Victor
  • 676
  • 1
  • 5
  • 17
  • thanks, but as I said, I don't want to use the html way to do this, I am looking for some solution that will generate the "readonly" into the html when reading metadata from the model – Edi Wang Feb 21 '13 at 06:10
  • @EdiWang sadly THIS IS TRUE. I was about to post it myself when answer came up. – Dave Alperovich Feb 21 '13 at 06:10
  • 1
    @EdiWang, LOL you are sooo right! Many developers have wasted many painfull hours finding this out! – Dave Alperovich Feb 21 '13 at 06:12
0

If you're using a SETTER you have to use { get; private set; }. That will make it so the client can't change the value. You can also just use an HTML 5 input and mark it there.