Is it possible using data annotations to add default value for int property
something like
[DefaultValue=1]
public int MyId {get; set;}
You can only do this using the class' constructor. Your code should thus look like this:
public class MyModel
{
public MyModel()
{
MyId = 1;
}
public int MyId {get; set;}
}
This will lead to the MyId
property being set to 1
whenever a new instance of the class is made. However, if model binding detects that the user has specified a value for MyId
, it will overwrite the default value with the user-specified one.