I have added several attributes to a property of a Model class. Some of the attributes work (e.g. DisplayName) but some atributes dont work (e.g. Required).
Here is the model class:
public classLoginModel
{
[Required]
[Display(Name = "User Name or Email")]
publicstring UserName { get;set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
publicstring Password { get;set; }
}
Here is the View razor code:
<div>
@Html.LabelFor(m => m.UserName)
</div>
<div>
@Html.EditorFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
Here is the Html generated from the View:
<div>
<labelfor="UserName">User Name or Email</label>
</div>
<div>
<inputclass="text-box single-line"data-val="true"data-val-required="The User Name or Email field is required."id="UserName"name="UserName"type="text"value=""/>
<spanclass="field-validation-valid"data-valmsg-for="UserName"data-valmsg-replace="true"></span>
</div>
When I run the app the Required validation does no work. The Model.IsValid is always true.
I have the proper steeings in the web,config
Any idea of why the Required validation wont work would be apprciated.
PS I have tried adding other attributes like StringLength and they dont work either