2013-05-25 47 views
2

我正在使用库服务堆栈,并且我有一个使用库ServiceStack.FluentValidation.Mvc3的问题,我按照步骤来配置这个库,以使得asp.net mvc引擎能够识别Model.IsValid财产,但这总是如此。这里是我的应用程序中验证设置的代码片段。model.isvalid mvc服务栈流利的验证

public class AppHost: AppHostBase 
{ 
    public AppHost() : base("Api Services", typeof(AppHost).Assembly) 
    { 
    } 
    //Provide extra validation for the registration process 
    public class CustomRegistrationValidator : RegistrationValidator 
    { 
     public CustomRegistrationValidator() 
     { 
      RuleSet(ApplyTo.Post,() => 
      { 
       RuleFor(x => x.DisplayName).NotEmpty().WithMessage("Ingresa tu nombre de usuario"); 
       RuleFor(x => x.LastName).NotEmpty().WithMessage("Ingresa tu apellido"); 
       RuleFor(x => x.Name).NotEmpty().WithMessage("Ingresa tu nombre"); 
      }); 
     } 
    } 

    public class CustomAuthValidator : AbstractValidator<Auth> 
    { 
     public CustomAuthValidator() 
     { 
      RuleFor(x => x.UserName).NotEmpty().WithMessage("Ingresa tu nombre de usuario").WithName("Nombre de Usuario"); 
      RuleFor(x => x.Password).NotEmpty().WithMessage("Ingresa tu contraseña").WithName("Contraseña"); 
     } 
    } 
    public override void Configure(Container container) 
    { 
     container.Adapter = new WindsorContainerAdapter(); 

     Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { 
      new CredentialsAuthProvider(),  
      new BasicAuthProvider()})); 


     Plugins.Add(new RegistrationFeature()); 
     Plugins.Add(new ValidationFeature()); 

     container.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>(); 
     container.RegisterAs<CustomAuthValidator, IValidator<Auth>>(); 

     FluentValidationModelValidatorProvider.Configure(); 

     container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("localhost:6379")); 
     container.Register(c => c.Resolve<IRedisClientsManager>().GetCacheClient()).ReusedWithin(Funq.ReuseScope.None); 

     container.Register<IResourceManager>(new ConfigurationResourceManager()); 



     ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container)); 
     ServiceStackController.CatchAllController = reqCtx => container.TryResolve<AccountController>(); 

    } 


    public static void Start() 
    { 
     new AppHost().Init(); 
    } 
} 

邮件错误是默认邮件错误不会更改我在代码中显示的标签名称。

这里是代码的摘录,看看模型是否有效,进入控制器Account

[HttpPost] 
public ActionResult LogOn(Auth model, string returnUrl) 
{ 
    //This always is true 
    if (ModelState.IsValid) 
    { 
     //TODO: Save database and other action more 
    } 
    return View(model); 
} 

请帮忙。

回答

2

这应该工作,虽然我不知道这是否是最好的解决办法

子类验证不和属性它与ValidatorAttribute

[Validator(typeof(CustomAuthValidator))] 
public class MyAuth : Auth 
{ 
} 

改变你CustomAuthValidator使用新MyAuth型

public class CustomAuthValidator : AbstractValidator<MyAuth> 
{ 
    public CustomAuthValidator() 
    { 
     RuleFor(x => x.UserName).NotEmpty().WithMessage("Ingresa tu nombre de usuario").WithName("Nombre de Usuario"); 
     RuleFor(x => x.Password).NotEmpty().WithMessage("Ingresa tu contraseña").WithName("Contraseña"); 
    } 
} 

更改您的控制器以采用新的MyAuth类型

[HttpPost] 
public ActionResult LogOn(MyAuth model, string returnUrl) 
{ 
    //This should now work as expected 
    if (ModelState.IsValid) 
    { 
     //TODO: Save database and other action more 
    } 
    return View(model); 
}