In my model:
[Required]
[DefaultValue("Some text ...")]
public string SomeValue{ get; set; }
In my view:
@Html.TextBoxFor(model => model.SomeValue)
At start the textbox is empty, I’d like it to contain the default value.
Thank you if you can help me.
You need to set the value in the constructor of your model…
The
DefaultValueAttributeisn’t for setting the value – it is for comparing it later. For example, when the model is mapped from the user input, you can check ifSomeValuehas been entered or not because you can check whether it is the same asDefaultValue.