MVC 4为可空类型添加验证 [英] MVC 4 adding validation for nullable type

查看:75
本文介绍了MVC 4为可空类型添加验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVC的行为感到惊讶。我有一个模特说A



  public   int  requiredField { get ;  set ;} 
< span class =code-keyword> public int ? notRequiredField { get ; set ;}





MVC验证requiredField字段是好的,但为什么验证notRequiredField字段,该字段未标记为[Required]或任何其他标记。



第二件事是,我用过int吗?在其他屏幕上的相同应用程序中发现没有问题,但在一个屏幕上它正在制造问题。



它是MVC还是我自己的错误?

解决方案

您好Faisalabadians,



尝试使用Nullable< int>而不是int?

这可以解决你我认为的一个问题。



问候,

RK

Hi, I am surprised with the behaviour of the MVC. I have a model say A

public int requiredField{get;set;}
public int? notRequiredField{get;set;}



MVC validating requiredField field which is good but why validating notRequiredField field which is not either marked as [Required] or any other tag.

The second thing is, I have used int? in the same application on other screens and found no issue with that but on one screen it is making issue.

is it a bug of MVC or my own?

解决方案

Hi Faisalabadians,

Try using Nullable<int> instead of int?
This could solve your one issue I believe.

Regards,
RK


这篇关于MVC 4为可空类型添加验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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