asp.net mvc的int属性绑定异常 [英] asp.net mvc int property bind exception

查看:151
本文介绍了asp.net mvc的int属性绑定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的类中的int属性,并要验证,如果用户输入的字符串。
我如何能做到用数据注解?
当我通过非整数值,我得到一个错误时抛出这样的:

 值'asdasd是无效的财产。

例如使用此验证属性:

  [范围(0,Int32.MaxValue,的ErrorMessage =无效号码)]
公众诠释?数{搞定;组; }

和进入现场塔'AAA'使用我有这个消息的excepetion模型:

 值'AAA'是无效号码。

取而代之的是无效号码的消息。

任何想法?


我已经把

  [范围(0,Int32.MaxValue,的ErrorMessage =无效号码)]
公众诠释?数{搞定;组; }

但我已经从错误时抛出了这个消息。

 值'AAA'是无效号码。


解决方案

有验证的两个阶段在这里打球。

在你的属性设置验证被调用之前,该框架首先尝试解析信息。

所以这里有一对夫妇在此基础上code例子:

  [范围(0,Int32.MaxValue,的ErrorMessage =无效号码)]
公众诠释?数{搞定;组; }

我没什么键入到中...

无效号码(框架将创建一个空的整数,您的验证规则失败)

我输入A进入中...

的值'A'是无效的号码。 (框架不能将A到一个可空INT,所以该框架验证规则失败,您的验证规则岂不检查。

** **的解决方案。

1 - 使用默认的消息活到你正在使用MVC 3 / .NET 4中,这使得它更容易忽略这些消息

2 - 排除从粘结剂的价值,所以不会导致错误(但你必须绑定它,自己去查吧)

  [绑定(不包括=mynumber的)]

3 - 使它成为一个字符串,在模型上,然后用的TryParse测试它,并添加自己的自定义模型误差

 如果(!Int32.TryParse(mynumber的出myInteger)){
    ModelState.AddModelError(mynumber的,这不是一个数字!);
}

实际上有许多解决方案,但我要说的是,选项3去了。

I have a int property in my class and want to validate if the user has entered a string. How can I do that using data annotations? When I pass a non-integer value I get a excpetion like this:

The value 'asdasd' is not valid for property.

For example using this validation attribute:

[Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")]
public int? Number { get; set; }

and entering 'aaa' in a field tha uses the model I've got an excepetion with this message:

The value 'aaa' is not valid for Number.

Instead of the "Invalid Number" message.

Any Ideas?


I've put

[Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")]
public int? Number { get; set; }

but I've got this message from an excpetion

The value 'aaa' is not valid for Number.

解决方案

There are two stages of validation at play here.

Before the validation set up in your attributes is called, the framework first attempts to parse the information.

So here are a couple of examples based on this code:

[Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")]
public int? Number { get; set; }

I type nothing into the box...

"Invalid Number" (Framework will create a null integer, your validation rule fails)

I type "A" into the box...

"The value 'A' is not valid for Number." (Framework cannot convert "A" into a nullable int, so the framework validation rule fails and your validation rule it not checked.

** Solutions **

1 - Live with the default message until you are using MVC 3 / .NET 4, which makes it easier to override these messages

2 - Exclude the value from the binder, so it won't cause an error (but you will have to bind it and check it yourself)

[Bind(Exclude="MyNumber")]

3 - Make it a string on the model, then test it with a TryParse and add your own custom model error (This is a reasonable practice and reminds us all why View Models are used rather than Domain Objects!)

if (!Int32.TryParse("MyNumber", out myInteger)) {
    ModelState.AddModelError("MyNumber", "That isn't a number!");
}

There are actually lots of solutions, but I would say go with option 3 for now.

这篇关于asp.net mvc的int属性绑定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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