我正在尝试将ID
字段设为只读。它是数据库中的标识字段,因此用户不会设置它。但他们希望看到它。如果分配给DataForm
,我在下面遗漏的内容仍允许对该值进行编辑。
public class StatusChoice : BindableBase
{
private int id;
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Editable(false,AllowInitialValue = false)]
public int ID
{
get { return id; }
set
{
id = value;
OnPropertyChanged();
}
}
}
答案 0 :(得分:18)
使用ReadOnly属性标记属性。
[ReadOnly(true)]
public decimal BodyMassIndex { get; private set; }
请点击以下链接了解更多信息 Has the behavior for DataAnnotations in asp.net mvc 3 changed?
答案 1 :(得分:6)
根据情况,您有两种选择。
ArrayList
以下是MSDN的说明
[Editable(false)] or [ReadOnly(true)]
https://msdn.microsoft.com/en-us/library/system.componentmodel.readonlyattribute%28v=vs.110%29.aspx
指定此属性绑定的属性是只读还是读/写。 无法更改标记为ReadOnlyAttribute设置为true或没有Set方法的成员。没有此属性或标记为ReadOnlyAttribute设置为false的成员是可读/写的,可以更改它们。默认值为No。
System.ComponentModel.ReadOnlyAttribute
指示数据字段是否可编辑。
数据字段上存在EditableAttribute属性表示用户是否应该能够更改字段的值。 此类既不强制也不保证字段可编辑。无论是否存在此属性,底层数据存储都可能允许更改字段。