问题描述
好吧,我的问题是来自fluentValidation的modelvalidator在我的项目中不起作用,并且无论验证状态如何,ModelState.IsValid始终为true,我预先使用了asp.net mvc 4,.net 4.5,thx. /p>
Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
FluentValidationModelValidatorProvider.Configure();
}
LoginViewModel
using FluentValidation.Attributes;
namespace ViewModel.Cuentas
{
[Validator(typeof(LoginViewModel))]
public class LoginViewModel
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
LoginViewModelValidator
using FluentValidation;
using FluentValidation.Results;
namespace ViewModel.Cuentas.Validadores
{
public class LoginViewModelValidator : AbstractValidator<LoginViewModel>
{
public LoginViewModelValidator()
{
RuleFor(x => x.UserName).NotEmpty().WithMessage("El Campo Usuario es Necesario");
RuleFor(x => x.Password).NotEmpty().WithMessage("El Campo Usuario es Necesario");
}
}
}
和我的客户经理
您的Validator
属性似乎具有错误的类型.你有:
[Validator(typeof(LoginViewModel))]
public class LoginViewModel
type参数应该是您的验证器类-LoginViewModelValidator
.就像这样:
[Validator(typeof(LoginViewModelValidator))]
public class LoginViewModel
Ok my problem is the modelvalidator from fluentValidation is not working in my project, and ModelState.IsValid is always true no matter the state of the validation, im using asp.net mvc 4, .net 4.5, thx in advance.
Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
FluentValidationModelValidatorProvider.Configure();
}
LoginViewModel
using FluentValidation.Attributes;
namespace ViewModel.Cuentas
{
[Validator(typeof(LoginViewModel))]
public class LoginViewModel
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
LoginViewModelValidator
using FluentValidation;
using FluentValidation.Results;
namespace ViewModel.Cuentas.Validadores
{
public class LoginViewModelValidator : AbstractValidator<LoginViewModel>
{
public LoginViewModelValidator()
{
RuleFor(x => x.UserName).NotEmpty().WithMessage("El Campo Usuario es Necesario");
RuleFor(x => x.Password).NotEmpty().WithMessage("El Campo Usuario es Necesario");
}
}
}
and my account controller
Your Validator
attribute appears to have the wrong type. You have:
[Validator(typeof(LoginViewModel))]
public class LoginViewModel
The type argument should be your validator class - LoginViewModelValidator
. So it would be like this:
[Validator(typeof(LoginViewModelValidator))]
public class LoginViewModel
这篇关于FluentValidation ModelState.IsValid始终为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!