Mark a field "Read Only" with Data Annotations

Refracted Paladin picture Refracted Paladin · May 9, 2013 · Viewed 20.4k times · Source

I am trying to make the ID field read only. It is an Identity field in the DB so the user will not be setting it. However they would like to see it. What am I missing as the below, when assigned to a DataForm still allows that value to be Edited.

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();
        }
    }
}

Answer

vijay picture vijay · May 9, 2013

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?