ASP.NET MVC - 选择要使用的验证注解 [英] ASP.NET MVC - Choose which validation annotations to use

查看:88
本文介绍了ASP.NET MVC - 选择要使用的验证注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有特性的模型是这样的:

I have a model with properties that look like this:

public class YourDetails {

  [Required(ErrorMessage = "Code is required")]
  [StringLength(10, ErrorMessage = "Code length is wrong", MinimumLength = 2)]
  [Range(0, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")]
  public int Code { get; set; }

}

用户界面验证设置通常的开箱即用的方式与不引人注目的JS验证插件。

The UI validation is setup the usual out of the box way with unobtrusive JS validation plugin.

问题:我有2个导航动作,背部和下一个。其次是罚款,确认火灾时,事情是错的,而当事情是正确的,即 .isValid()返回true,将数据传递到数据库服务等等等等。

The issue: I have 2 navigation actions, back and next. Next is fine, validation fires when things are wrong, and when things are right i.e. .isValid() returns true, the data is passed to the DB service etc etc.

然而,当我preSS'回来'我有一个要求验证表单/视图模型不同之前储蓄。即确保 code 是一个正整数,但不与要求或<$ C $打扰C> StringLength 验证。

However when I press 'back' I have a requirement to validate the form/ViewModel differently prior to saving. I.e. make sure Code is a positive integer, but don't bother with the Required or StringLength validation.

所以基本上我想下一步,但部分位于背板全面验证。这可能吗?

So basically I want to validate fully on Next but partially on Back. Is that possible?

推荐答案

当我做了类似的事情在过去,我发现是使用流利的验证的 HTTP://fluentvalidation.$c$cplex.com/wikipage标题= MVC 。您可以将参数传递给验证器,切换到不同的规则集。

When I've done something similar in the past the easiest way i found was to use fluent validation http://fluentvalidation.codeplex.com/wikipage?title=mvc. You can pass parameters to the validator and switch to different rule sets.

这篇关于ASP.NET MVC - 选择要使用的验证注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆