Search code examples
asp.net-mvc-5entity-framework-6unobtrusive-validation

MVC5 unobtrusive validations not working


I am using entity framework with database first approach.

I have created separate class for validations.

[MetadataType(typeof(RoleMetaData))]
    public partial class Role
    {

    }

    class RoleMetaData
    {
        [Required(ErrorMessage = "Please enter role name")]
        public string Name { get; set; }
    }

and my html form:

@using (Html.BeginForm("Create", "Role", FormMethod.Post, new {  @class = "submitform" }))
{
 @Html.TextBoxFor(x => x.Name, new { @class="form-control" })
<input type="submit" class="btn btn-success width-150" value="Save" />
}

but html rendering to browser without any data-* attributes like:

<input class="form-control" id="Name" name="Name"  type="text" value="">

I am wondering why it is not rendering data-* attributes.

Please guide me what I am doing wrong.

Thanx.


Solution

  • I found out where the problem was, the namespace of my validation class is different from namespace where data entity exist.