mvc DataAnnotations how to make field no editable

2019-06-13 17:15发布

问题:

I have a few field in my entity that i wish to be non-editable. Looking in the docs it seems like "EditableAttribute" would do the trick. However this is only 4.0

Just wondering if there are other attributes that would have the desire effect. So be clear,

i have a field called "DateRegistered" i wish to display this as string not text field using "Html.EditorFor"

回答1:

The [ReadOnly] attribute should work in 3.5.



回答2:

Why are you using an editor template for something that should be read only? Display templates and the <%= Html.DisplayFor(x => x.DateRegistered) %> method seem more appropriate in this case.