问题描述
即时通讯使用MVC与数据注释启用。在我的ViewModel我有一个包含一个普通的前pression验证规则,只允许数字的属性。
Im using MVC with Data Annotations enabled. In my ViewModel I have a property that contains a regular expression validation rule to only allow numbers
[RegularExpression("@([0-9])", ErrorMessage = "Invalid. Must use only numbers.")]
当我提交表单ModelState.IsValid正在恢复为假。当我在控制器中添加一个断点在我的ModelState我看到了,我的装饰与常规的前pression数据注解是失败(即使我进入表单字段中的数字84)视图模型的StreetNumber属性。
When I submit my form the ModelState.IsValid is returning as false. When I added a breakpoint on my ModelState in the controller I saw that the StreetNumber property of the ViewModel that I decorated with the regular expression data annotation was failing (even though I entered the number 84 in the form field).
有没有人有任何想法,为什么这仍然会失败。 (我已经验证,这家酒店是被errounously失败的唯一的一个)。
Does anyone have any ideas as to why this would still be failing. (I have verified that this property was the only one that was errounously failing).
推荐答案
我想通了。它原来是一个非常模糊的监督。对于普通的前pression的DataAnnotation应该有引号前的@。
I figured it out. It turned out to be a very obscure oversight. The DataAnnotation for the Regular expression should have the @ before the quotation marks.
代替这样的:
[RegularEx pression(@(^ [0-9] + $)的ErrorMessage =无效的。只能使用数字。)]
Instead of this:[RegularExpression("@(^[0-9]+$)", ErrorMessage = "Invalid. Must use only numbers.")]
我需要有这样的:
[RegularEx pression(@(^ [0-9] + $)的ErrorMessage =无效的。只能使用数字。)]
I need to have this:[RegularExpression(@"(^[0-9]+$)", ErrorMessage = "Invalid. Must use only numbers.")]
我真的需要获得更大的咖啡杯:)
I really need to get a bigger coffee mug :)
这篇关于验证仍然不能解决普通EX pression,只允许数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!