客户端验证未烧制CompareAttribute DataAnnotation [英] Client-side validation not firing for CompareAttribute DataAnnotation

查看:101
本文介绍了客户端验证未烧制CompareAttribute DataAnnotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我奠定了比较两个字符串密码的视图。在我的车型之一这两个属性是pretty简单:

  [必需]
    [RegularEx pression(@(\\ S)+的ErrorMessage =白色空间是不允许)]
    [StringLength(20,MinimumLength = 6)]
    [数据类型(DataType.Password)
    [显示(NAME =新密码)]
    公共字符串NEWPASSWORD {搞定;组; }    [需要]
    [数据类型(DataType.Password)
    [RegularEx pression(@(\\ S)+的ErrorMessage =白色空间是不允许)]
    [StringLength(20,MinimumLength = 6)]
    [显示(NAME =确认密码)]
    [比较(新密码的ErrorMessage =新密码和确认密码不匹配。)
    公共字符串ConfirmPassword {搞定;组; }

下面是我的看法code:

 <表类=字段集中心WIDTH =400>
    <&TBODY GT;
        &所述; TR>
            百分位宽度=150>
                @ Html.LabelFor(M = GT; m.NewPassword)
            < /第i
            &所述; TD>
                @ Html.PasswordFor(M = GT; m.NewPassword,新{@class =itext3})
                < BR />< BR />@Html.ValidationMessageFor(m => m.NewPassword)
            < / TD>
        < / TR>
        &所述; TR>
            百分位宽度=150>
                @ Html.LabelFor(M = GT; m.ConfirmPassword)
            < /第i
            &所述; TD>
                @ Html.PasswordFor(M = GT; m.ConfirmPassword,新{@class =itext3})
                < BR />< BR />@Html.ValidationMessageFor(m => m.ConfirmPassword)
            < / TD>
        < / TR>
    < / TBODY>
< /表>

测试时,除了这不是射击,直到我打了服务器CompareAttribute上ConfirmPassword所有属性的火他们的客户端验证消息。然而,在我控制器ModelState.IsValid = FALSE。

我对比我在做什么到正常工作的默认MVC应用程序。用于故障诊断和修复这个有什么建议?

我使用MVC 3 RTM。


解决方案

看看你的_Layout.cshtml脚本标记。我猜这个问题可能是你的jQuery引用。什么时候开始的MVC 3项目的形式划痕或您使用的一个例子项目或类似的东西?

下面是我发生了什么;我有类似的问题...


  • 我用一些例子code,它指出ajax.microsoft.com在src属性

  • 因此,例如脚本标签之一是这样的:&LT;脚本src=\"http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js\"></script>

  • 我要得到什么JS是在客户端执行更好地处理,所以我把它改成这样:&LT;脚本的src =@ Url.Content(〜/脚本/ jQuery的。 validate.min.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;

  • 这只是一个例子,我想有,我也改变了一对夫妇更脚本标记

所以,要改变后,在内部担任jQuery的文件,它的工作。我回去,看着我的本地.validate.js文件......,这是1.6版本。这就是我意识到这个问题是因为jQuery的版本或它与JS库反正其他的相容性。

底线是,它看起来像1.7不完全符合validate.unobtrusive.js lib中,我不得不发挥作用... ...有可能是不函数1.7的新版本......就像我说的,我是用一个例子项目修修补补所以有一些未知那里。我想这也可能是它与其它JS库之一的MvcValidation.js lib中不兼容呢?

在任何情况下,我会说,以说明你的问题,最简单的方法是,你是最有可能引用的JS库的一个糟糕的组合。我想说的最好的故障安全的方式来获得的js库的一个很好的结合是建立在Visual Studio中一个新的Asp.Net MVC 3项目,看看是什么版本,它可以让你在默认情况下/与项目模板......这是假设你没有从头开始的项目。如果你没有从头开始,那么它可能会改变你的布局文件,以有不良JS引用或者如果没有那是真的话,我想这可能是与VisualStudio的项目模板一个问题?......实事求是地我会说,是值得怀疑的,但。所有这一切说 - 我想说的最可能的原因[我会下注反正]就是你刚刚麻烦像我这样想快速地使用一些例如code =)<​​/ P>

I'm laying out a view that compares two password strings. The two properties in one of my models are pretty straightforward:

    [Required]
    [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
    [StringLength(20, MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New Password")]
    public string NewPassword { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
    [StringLength(20, MinimumLength = 6)]
    [Display(Name = "Confirm Password")]
    [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

Here's my view code:

<table class="fieldset center" width="400">
    <tbody>
        <tr>
            <th width="150">
                @Html.LabelFor(m => m.NewPassword)
            </th>
            <td>
                @Html.PasswordFor(m => m.NewPassword, new { @class = "itext3" })
                <br /><br />@Html.ValidationMessageFor(m => m.NewPassword)
            </td>
        </tr>                       
        <tr>
            <th width="150">
                @Html.LabelFor(m => m.ConfirmPassword)
            </th>
            <td>
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "itext3" })
                <br /><br />@Html.ValidationMessageFor(m => m.ConfirmPassword)
            </td>
        </tr>
    </tbody>
</table>

All of the attributes fire their client-side validation messages when tested, except for the CompareAttribute on ConfirmPassword which is not firing until I hit the server. However, in my controller the ModelState.IsValid = false.

I compared what I'm doing to the default MVC application which is working correctly. Any suggestions for troubleshooting and fixing this?

I'm using MVC 3 RTM.

解决方案

Take a look at your script tags in your _Layout.cshtml. I'm guessing the issue is probably your jQuery references. Did you start the MVC 3 project form scratch or are you using an example project or something like that?

Here is what happend with me; I had similar issues...

  • I was using some example code that pointed to ajax.microsoft.com in the src attributes
  • So, for example one of the script tags looked like this: <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
  • I wanted to get a better handle on what js was executing on the client side so I changed it to this: <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
  • That is just an example, I think there were a couple more script tags that I changed as well

So, after changing to internally served jQuery files it worked. I went back and looked at my local .validate.js file... and it was version 1.6. That is how i realized the issue was due to the version of jQuery or it's compatibility with one of the other the js libs anyway.

Bottom line is that it looks like 1.7 doesn't fully function with the validate.unobtrusive.js lib that I had... there may be a newer version that does function with 1.7... like I said, I was tinkering with an example project so there are some unknowns there. I suppose it could also be an incompatibility with the MvcValidation.js lib between it and one of the other js libs too?

At any rate, I'd say the simplest way to state your issue is that you are most likely referencing a bad combination of js libs. I'd say the best failsafe way to get a good combination of js libs is to create a new Asp.Net MVC 3 Project in Visual Studio and see what versions it gives you by default/with the project template... that is assuming that you didn't start the project from scratch. If you DID start it from scratch then may be you changed your layout file to have bad js references or if none of that is true then I suppose it could be a problem with the VisualStudio project templates?... realistically I'd say that is doubtful though. All of that said- I'd say the most likely cause [that I'd wager on anyway] is that you just got in trouble like me trying to quickly use some example code =)

这篇关于客户端验证未烧制CompareAttribute DataAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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