MVC3属性验证问题 [英] MVC3 Attribute validation question

查看:162
本文介绍了MVC3属性验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到与我在我看来验证奇怪的行为。

I'm getting odd behavior with my validation in my view.

我的模型有这个属性。

    [Display(Name = "Overflow Capacity")]
    [RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")]
    [Range(0,9999.99,ErrorMessage = "Value must be between 0 - 9,999.99")]
    public decimal OverFlowCapacity { get; set; }

我的看法有这样的:

My view has this:

<tr>
   <td>@Html.LabelFor(m=> m.OverFlowCapacity)</td>
   <td>@Html.EditorFor(m=>m.OverFlowCapacity)</td>                                               
   <td> @Html.ValidationMessageFor(model => model.OverFlowCapacity)</td>
</tr>

如果我进入像'ABC'的值,我得到验证消息所需数量
如果我输入999999值,我得到验证消息值必须介于0 - 9,999.99

If I enter a value like 'ABC', I get the validation message 'Number required' If I enter a value of 999999, I get the validation message 'Value must be between 0 - 9,999.99'

这两种这些消息都当如预期我关闭标签的文本框中好评。
当我离开文本框的值空和标签的时候,我没有错误,符合市场预期。

Both of those messages are received when I tab off the text box as expected. When I leave the text box value empty and tab off, I get no errors, as expected.

然而,当我提出,我得到一个验证消息的溢出容量字段是必需的。

However, when I submit, I get a validation message 'The Overflow Capacity field is required.'

我不知道这是来自。我试着从模型中取出所有验证属性,并仍然得到必要的消息。我不知所措。

I don't know where this is coming from. I've tried removing all validation attributes from the model, and still get the 'required' message. I'm at a loss.

下面是我所引用的脚本。

Here are the scripts I've referenced.

我有 mvcfoolproof ,我以后可能会发布。我不知道这是不是在某种程度上负责我的问题。

I have other issues with mvcfoolproof that I may post later. I'm wondering if this isn't somehow responsible for my problems.

推荐答案

现在所发生的事情,你是后验证踢的形式提交后,并确定该小数值不能。现在你正在使用 A decimal类型是不可为空。如果你想要这个行为,你想看到验证您提交表单之前那么 [必需] 属性添加到属性。然而,如果你的不要想要这个功能,它都不可能,然后从小数更改类型小数可空&LT;小数方式&gt;

What's happening to you now is the post validation is kicking in after the form has been submitted and determining that the decimal value cannot be null. Right now you are using a decimal type which is non-nullable. If you want this behavior and you want to see the validation before you submit the form then add the [Required] attribute to the property. However if you don't want this functionality and it can possibly be null, then change your type from decimal to decimal? or Nullable<decimal>.

不允许空值和有pre-提交验证:

[Display(Name = "Overflow Capacity")]
[RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")]
[Range(0,9999.99,ErrorMessage = "Value must be between 0 - 9,999.99")]
[Required]
public decimal OverFlowCapacity { get; set; }

允许空值和摆脱后提交验证错误的:

[Display(Name = "Overflow Capacity")]
[RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")]
[Range(0,9999.99,ErrorMessage = "Value must be between 0 - 9,999.99")]
public decimal? OverFlowCapacity { get; set; }

这篇关于MVC3属性验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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