user1765862 user1765862 - 2 months ago 27
C# Question

Default value in mvc model using data annotation

Is it possible using data annotations to add default value for int property

something like

[DefaultValue=1]
public int MyId {get; set;}

Answer

Try this

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

    public YOURMODEL()
    { 
        MyId = 1;       
    }
}