Mark a field “Read Only” with Data Annotations

◇◆丶佛笑我妖孽 提交于 2019-12-05 00:36:51
vijay

Mark Property with ReadOnly attribute.

[ReadOnly(true)]
public decimal BodyMassIndex { get; private set; }

Follow below link for more Has the behavior for DataAnnotations in asp.net mvc 3 changed?

You have two options in general based on situation.

[Editable(false)] or [ReadOnly(true)]

Below are descriptions from MSDN

System.ComponentModel.ReadOnlyAttribute

https://msdn.microsoft.com/en-us/library/system.componentmodel.readonlyattribute%28v=vs.110%29.aspx

Specifies whether the property this attribute is bound to is read-only or read/write. Members that are marked with the ReadOnlyAttribute set to true or that do not have a Set method cannot be changed. Members that do not have this attribute or that are marked with the ReadOnlyAttribute set to false are read/write, and they can be changed. The default is No.

System.ComponentModel.DataAnnotations.EditableAttribute

https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.editableattribute%28v=vs.110%29.aspx

Indicates whether a data field is editable.

The presence of the EditableAttribute attribute on a data field indicates whether the user should be able to change the field's value. This class neither enforces nor guarantees that a field is editable. The underlying data store might allow the field to be changed regardless of the presence of this attribute.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!