.NET 4 MVC 2的验证与注释警告而不是错误 [英] .NET 4 MVC 2 Validation with annotations warning instead of error

查看:174
本文介绍了.NET 4 MVC 2的验证与注释警告而不是错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET 4 MVC 2与注解验证。有没有给回警告而不是在错误(简单的)解决方案?所以,我能得到一个绿色或黄色的框,这样的消息:你不应该使用这个内容,但是你可能。

I am using .NET 4 with MVC 2 for validating with Annotations. Is there a (simple) solution for giving back a warning instead of the error? So that I am able to get a green or yellow box with a message like "you should not use this content, but you may".

大在此先感谢! :)

编辑:的结果
请注意,我已经能够通过的ErrorMessage 抛出了错误,但我此外想是这样 WarningMessage InfoMessage ,以便用户只得到一个警告,但可能会继续进行。是否有解决方案为这个?


Please observe that I am already able to throw out errors via ErrorMessage but I additionally want something like WarningMessage or InfoMessage so that the user only gets a warning but might proceed. Is there a solution for this?

伪code将是:
(请注意,伪,因为 WarningMessage 是(不幸)不是有效的类)

The Pseudocode would be: (Please note the "pseudo", because WarningMessage is (unfortunately) not a valid class)

public class Person
{
  [StringLength(50)]
  [Required(ErrorMessage = "You MUST enter a name!")]
  public string Name { get; set; }

  [Required(WarningMessage = "It is recommended to fill out the age but you may leave it out)]
  public int Age { get; set; }
}

我想有这个centrallized在我的验证类 - 不显山露水在.js文件

And yes I want to have this centrallized in my validation class - not somewhere in a .js-file.

推荐答案

我会第一时间迅速评论说,我不知道这是否是一个好主意!

必需属性有一定的字符 - 人们期待它的某些事情时,他们包括这对他们的属性。如果你想使用属性来定义这种行为,试图描述你在做什么更准确,所以不是你的:

"Required" attribute has a certain "character" - people expect certain things of it when they include this on their attributes. If you want to use attributes to define this behaviour, try to describe what you're doing more exactly, so instead of your:

[Required(WarningMessage = "It is recommended to fill out age...")]
public int Age { get; set; }

我有:

[PreSubmitWarningMessage("It is recommended to fill out age...")]
public int Age { get; set; }

您可以创建自己的属性,这些属性会影响该请求的处理方式,但像标准的MVC属性,这样做会不会传播到客户端的验证。

You can create your own attributes that'll affect how the request is handled, though this won't propagate to client-side validation like the standard MVC attributes do.

要获得此发挥好与MVC中,你需要创建这个作为一个行动过滤器 - 通过实施ActionFilterAttribute并重写OnActionExecuted方法具体是:

To get this to play nice with MVC, you need to create this as an Action Filter - specifically by implementing the ActionFilterAttribute and overriding the OnActionExecuted method:

public class PreSubmitWarningMessage : ActionFilterAttribute
{    
    private string _warningMessage;

    public PreSubmitWarningMessage(string warningMessage)
    {
        _warningMessage = warningMessage;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // stuff you do here will happen before the 
        // controller action is executed.

        // you can compare the private warning message 
        // to the metadata in filterContext
    }
}

您可以做几件事情在这里,我不知道什么最好的做法是,但在hackiest,您可以访问在filterContext模型,这样你就可以改变你的控制器操作的行为,更新您的视图模型到一定的状态,如果你的警告条件(这种情况下,该字段是必需的)。

You could do a couple of things in here, I'm not sure what best practice would be, but at the hackiest, you have access to the model in filterContext, so you can change the behaviour of your controller action, updating your view model to a certain state if your warning condition (for this case, that the field is required).

人们可能会做出只延长一RequiredAttribute标签的情况下,但我不认为这是正确的说,你的新属性的 RequiredAttribute标签一个,这样的传承不会是正确的概念。

People might make a case for just extending the RequiredAttribute, but I don't think it's correct to say that your new Attribute IS a RequiredAttribute, so inheritence wouldn't be conceptually correct.

这篇关于.NET 4 MVC 2的验证与注释警告而不是错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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