Why does fluent.validate throw an exception in asp.net mvc
i want to overr开发者_如何学编程ide the default asp.net-mvc validation when posting a form so i tried using fluent.validation
i created a validator class (ProjectValidator)
public class ProjectValidator : AbstractValidator<Project>
{
public ProjectValidator()
{
RuleFor(h => h.Application).NotNull().WithName("Application");
RuleFor(h => h.FundingType).NotNull().WithName("Funding Type");
RuleFor(h => h.Description).NotEmpty().WithName("Description");
RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
}
}
i put an attribute on my Data Transfer Object class
[Validator(typeof(ProjectValidator))]
public class ProjectViewModel
{
...
}
and i put this in application_start();
DataAnnotationsModelValidatorProvider
.AddImplicitRequiredAttributeForValueTypes = false;
ModelValidatorProviders.Providers.Add(
new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
but when i post a form that uses this object, i get the following error:
Method not found: 'System.Collections.Generic.IEnumerable`1 FluentValidation.IValidatorDescriptor.GetValidatorsForMember(System.String)'.
any suggestions?
It could be a problem related to the assembly versions you are using. Here are the steps that work for me with the latest version of FluentValidation.NET and ASP.NET MVC 3:
- Create a new ASP.NET MVC 3 project using the default Visual Studio template.
- Install the
FluentValidation.MVC3
NuGet package. Add the view model and its corresponding validator (notice that in your case you have the validator for type Project -
AbstractValidator<Project>
whereas your view model is calledProjectViewModel
which is inconsistent. The validator must be associated to the view model):public class ProjectValidator : AbstractValidator<ProjectViewModel> { public ProjectValidator() { RuleFor(h => h.Application).NotNull().WithName("Application"); RuleFor(h => h.FundingType).NotNull().WithName("Funding Type"); RuleFor(h => h.Description).NotEmpty().WithName("Description"); RuleFor(h => h.Name).NotEmpty().WithName("Project Name"); } } [Validator(typeof(ProjectValidator))] public class ProjectViewModel { public string Application { get; set; } public string FundingType { get; set; } public string Description { get; set; } public string Name { get; set; } }
In
Application_Start
register the validator:protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory())); }
Define a controller:
public class HomeController : Controller { public ActionResult Index() { return View(new ProjectViewModel()); } [HttpPost] public ActionResult Index(ProjectViewModel model) { return View(model); } }
And a view:
@model Appame.Models.ProjectViewModel @using (Html.BeginForm()) { <div> @Html.LabelFor(x => x.Application) @Html.EditorFor(x => x.Application) @Html.ValidationMessageFor(x => x.Application) </div> <div> @Html.LabelFor(x => x.FundingType) @Html.EditorFor(x => x.FundingType) @Html.ValidationMessageFor(x => x.FundingType) </div> <div> @Html.LabelFor(x => x.Description) @Html.EditorFor(x => x.Description) @Html.ValidationMessageFor(x => x.Description) </div> <div> @Html.LabelFor(x => x.Name) @Html.EditorFor(x => x.Name) @Html.ValidationMessageFor(x => x.Name) </div> <input type="submit" value="OK" /> }
精彩评论