Default value in mvc model using data annotation

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

Leave a Comment

bahis casinocanlı casino siteleritürkçe altyazılı pornocanlı bahis casino