I am trying to use Client Side Validation in MVC using data annotations. Data val attribute is not generated and client side validation is not working.
我正在尝试使用数据注释在MVC中使用客户端验证。不生成数据val属性,并且客户端验证不起作用。
My approach is :
我的方法是:
public partial class User : BaseEntity
{
public User()
{
this.UserRoles = new List<UserRole>();
}
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
I am using above class as partial class extension:
我使用上面的类作为部分类扩展:
[MetadataType(typeof(UserMetaData))]
public partial class User
{ }
public class UserMetaData
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
Including View :
包括视图:
@using (Html.BeginForm("Add", "user", FormMethod.Post, new { @class = "form-horizontal"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.Partial("_UserDetails", Model)
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<div class="col-md-12 col-sm-12 col-xs-12 pull-left">
<input type="submit" class="btn btn-primary btn_box" value="Add" id="formSubmit" />
</div>
</div>
</div>
}
_UserDetails Partial View :
_UserDetails局部视图:
<div class="form-group">
<div class="col-md-4 col-sm-12 col-xs-12">@Html.Label("First Name :", new { @class = "col-sm-12 control-label text-label" })</div>
<div class="col-md-8 col-sm-12 col-xs-12">@Html.TextBoxFor(m => m.User.FirstName, new { @class = "form-control input-fild", @id = "firstname" })</div>
</div>
<div class="form-group">
<div class="col-md-4 col-sm-12 col-xs-12">@Html.Label("Last Name :", new { @class = "col-sm-12 control-label text-label" })</div>
<div class="col-md-8 col-sm-12 col-xs-12">@Html.TextBoxFor(m => m.User.LastName, new { @class = "form-control input-fild", @id = "lastname" })</div>
</div>
When i run the application and see view source "data val" attribute is not generated on the control as it use to generate in normal [Required] attribute.
当我运行应用程序并看到视图源“数据val”属性未在控件上生成,因为它用于生成正常的[必需]属性。
Am i missing anything ? Thanks.
我错过了什么吗?谢谢。
2 个解决方案
#1
Can you have a look at the solutions provided for this question. MVC 4 client side validation not working
你能看看为这个问题提供的解决方案吗? MVC 4客户端验证无法正常工作
Also another one is described in this blog
此博客中还描述了另一个
#2
Did you enabled client-side validation in Web.config?
您是否在Web.config中启用了客户端验证?
<appSettings>
<add key="ClientValidationEnabled" value="true" />
</appSettings>
#1
Can you have a look at the solutions provided for this question. MVC 4 client side validation not working
你能看看为这个问题提供的解决方案吗? MVC 4客户端验证无法正常工作
Also another one is described in this blog
此博客中还描述了另一个
#2
Did you enabled client-side validation in Web.config?
您是否在Web.config中启用了客户端验证?
<appSettings>
<add key="ClientValidationEnabled" value="true" />
</appSettings>