Default value in mvc model using data annotation

.net, asp.net-mvc, c#

Solution

Try this - set the default value in the constructor:

public class YOURMODEL
{
    public int MyId { get; set; }  

    public YOURMODEL()
    { 
        MyId = 1;       
    }
}

Later addition by other user: Since C# 6.0 (2015) this simpler syntax has been allowed:

public class YOURMODEL
{
    public int MyId { get; set; } = 1;
}

Problem

Is it possible using data annotations to add default value for int property something like ``` [DefaultValue=1] public int MyId {get; set;} ```

Original source

Related problems