ASP .Net MVC应用程序
问题描述:
的流畅的验证休息时间我一直在面对这个问题,并尝试了各种各样的方面。到现在为止没有任何工作。这是我的。我有一个Student.cs域模型类。ASP .Net MVC应用程序
[FluentValidation.Attributes.Validator(typeof(StudentViewModelValidator))]
public class Student
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int StudentId { get; set; }
public String LastName { get; set; }
public String FirstName { get; set; }
public String UserName { get; set; }
public String Password { get; set; }
[ForeignKey("Department")]
public int DepartmentID { get; set; }
//Department Navigational Property
public Department Department { get; set; }
}
并有StudentViewModel类:
//View Model
public class StudentViewModel
{
public Student Student { get; set; }
[Display(Name = "StudentName")]
public String StudentFullName
{
get
{
return String.Format("{0} {1}", Student.FirstName, Student.LastName);
}
}
[DataType(DataType.Password)]
public String Password { get; set; }
[DataType(DataType.Password)]
[Compare("Password",ErrorMessage="Passwords do not match")]
[Display(Name="Confirm Password")]
public String C_Password { get; set; }
[Display(Name = "Department Name")]
public String DepartmentName { get; set; }
}
部Model类的样子:
public class Department
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DepartmentID { get; set; }
//[Required(ErrorMessage="Department Name cannot be empty")]
public String DepartmentName { get; set; }
//[Required(ErrorMessage = "Department Code cannot be empty")]
public String DepartmentCode { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
的FluentValidation规则是这样的:
public class StudentViewModelValidator : AbstractValidator<Student>
{
public StudentViewModelValidator()
{
RuleFor(m => m.FirstName)
.NotEmpty().WithMessage("First Name is required.")
.Matches(@"^\D*$").WithMessage("Special characters and numbers are not allowed in First Name");
RuleFor(m => m.LastName)
.NotEmpty().WithMessage("Last Name is required.")
.Matches(@"^\D*$").WithMessage("Special characters and numbers are not allowed in Last Name");
RuleFor(m => m.UserName)
.NotEmpty().WithMessage("User Name is required.")
.Matches(@"^a-zA-Z0-9\.\$").WithMessage("Only . and $ are allowed special characters in a user name");
RuleFor(m => m.Password)
.NotEmpty().WithMessage("Password is required.")
.Length(4, 10).WithMessage("Password should be 4 to 10 characters long")
.Matches(@".*\d{2}").WithMessage("Password should contain at least 2 digits")
.Matches(@".*a-zA-Z").WithMessage("Password should contain at least one albhabet")
.Matches(@"[\.\$\~\&]").WithMessage("Allowed special characters in a password are . $ ~ &");
}
}
验证不断破坏称为
不引人注意的客户端验证规则中的验证类型名称必须是唯一的。以下验证类型不止一次出现:正则表达式
at Password
field。
观的样子:
@model MvcApplication4.Models.Student
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend><strong>Create a record</strong></legend>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DepartmentID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DepartmentID)
@Html.ValidationMessageFor(model => model.DepartmentID)
</div>
<p>
<input type="submit" class="createDetails" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
更新
如果我把这些线在Application_Start()
//var fluentValidationModelValidatorProvider = new FluentValidationModelValidatorProvider(new AttributedValidatorFactory());
//ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider);
//DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
//fluentValidationModelValidatorProvider.AddImplicitRequiredValidator = false;
然后验证处断裂LastName
字段。
请通过帮助我。
答
此问题可能是由于价值倍数设置为相同的密钥
由于ValidatonMessage是一本字典的类型。
删除视图模型上的所有装饰/数据注释,并使用fluentvalidator,它将继续工作。
请参考
Validation type names in unobtrusive client validation rules must be unique
是否只有一个在密码验证.matches工作? – Duston
不,它没有。它仍然在LastName中显示错误。当我从FluentValidator注释到LastName的代码块时,它显示FirstName的错误。 – StrugglingCoder