在asp.net mvc的4 DataAnnotations验证(普通防爆pression) - 剃刀视图 [英] DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view

查看:158
本文介绍了在asp.net mvc的4 DataAnnotations验证(普通防爆pression) - 剃刀视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该DataAnnotations验证不是在asp.net mvc的4剃刀视图中工作,在常规的前pression使用特殊字符时。

The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the special characters in the regular expression.

型号:

[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
public string FirstName { get; set; }

的Razor视图:

@Html.TextBoxFor(model => Model.FirstName, new { })
@Html.ValidationMessageFor(model => Model.FirstName)

不显眼的审定是鉴于呈现为:

The unobtrusive validation is rendered in view as:

<input type="text" value="" tabindex="1" style="height:auto;" name="FirstName" maxlength="100" id="FirstName" data-val-regex-pattern="^([a-zA-Z0-9 .&amp;amp;&amp;#39;-]+)$" data-val-regex="Invalid First Name" data-val-length-max="100" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val="true" class="textfield ui-input-text ui-body-d ui-corner-all ui-shadow-inset valid">

作为模型的RegularEx pression,输入正确的数据,即使这会导致在错误中指定在上面的html的正则表达式不呈现(山姆

我该如何处理呢?

- 更新 -

我已经更新了code按@Rick建议

I have updated the code as per @Rick suggestion

[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("([a-zA-Z0-9 .&'-]+)", ErrorMessage = "Enter only alphabets and numbers of First Name")]
public string FirstName { get; set; }

查看源代码显示如下:

<input data-val="true" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val-length-max="100" data-val-regex="Enter only alphabets and numbers of First Name" data-val-regex-pattern="([a-zA-Z0-9 .&amp;amp;&amp;#39;-]+)" id="FirstName" maxlength="100" name="FirstName" type="text" value="" />

我仍然有同样的问题。

Still i have the same issue.

推荐答案

更新9 2012年7月 - 看起来这是固定的RTM。
1.我们已经暗示 ^ $ ,所以你不需要添加。 (它不会似乎对包括他们的问题,但你并不需要它们)
 2.这似乎是在ASP.NET MVC 4 / preVIEW / Beta版中的错误。我已经开了一个bug

UPDATE 9 July 2012 - Looks like this is fixed in RTM. 1. We already imply ^ and $ so you don't need to add them. (It doesn't appear to be a problem to include them, but you don't need them) 2. This appears to be a bug in ASP.NET MVC 4/Preview/Beta. I've opened a bug

查看源代码显示如下:

data-val-regex-pattern="([a-zA-Z0-9 .&amp;&#39;-]+)"                  <-- MVC 3
data-val-regex-pattern="([a-zA-Z0-9&#32;.&amp;amp;&amp;#39;-]+)"      <-- MVC 4/Beta

看起来我们是双重编码。

It looks like we're double encoding.

这篇关于在asp.net mvc的4 DataAnnotations验证(普通防爆pression) - 剃刀视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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