MVC 4客户端验证无效。

时间:2022-12-09 13:41:11

Trying to learn the basics of ASP.net MVC and cant quite understand why my client side validation isnt working.

尝试学习ASP.net MVC的基础知识,并不能完全理解为什么我的客户端验证不起作用。

I have this in both my web.config files (1 is global 1 is in the views folder)

我的网站上有这个。配置文件(1是全局1在views文件夹中)

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

I have this in my _layout.cshtmlfile

我在_layout.cshtmlfile中有这个。

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/jquery.validate.min.js")

This is my view model for which im testing registration:

这是我测试注册的视图模型:

public class RegisterVM
    {
        [Required(ErrorMessage="Username Required")]
        public string username { get; set; }

        [RegularExpression(@"((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})")]
        [Required(ErrorMessage="Password Required")]
        public string password { get; set; }

        [Compare("password", ErrorMessage="Passwords do not match")]
        public string passwordConfirm { get; set; }
    }

And this is my html

这是我的html。

@using(@Html.BeginForm()){

    @Html.LabelFor( model => model.username)
    @Html.EditorFor( model => model.username)
    @Html.ValidationMessageFor( model => model.username)<br />

    @Html.LabelFor( model => model.password)
    @Html.PasswordFor( model => model.password)
    @Html.ValidationMessageFor( model => model.password)<br />

    @Html.LabelFor( model => model.passwordConfirm)
    @Html.EditorFor( model => model.passwordConfirm)
    @Html.ValidationMessageFor( model => model.passwordConfirm)<br />

    <input type="submit" value="Register" />


}

Didn't want to post here, ive read a few topics on here related but cant seem to fix my issues, when i click submit it's making post/get requests.

我不想在这里发表文章,我在这里读了一些相关的话题,但似乎无法解决我的问题,当我点击提交的时候,我的问题就变成了post/get请求。

6 个解决方案

#1


22  

Feels like this should work, I think you need to load validate.min.js before validate.unobtrusive.min.js

感觉应该这样,我认为您需要加载确认器。js validate.unobtrusive.min.js之前

#2


10  

I had a similar problem and the only thing that fixed it was to manually call

我有一个类似的问题,唯一的解决办法就是手动调用。

 $.validator.unobtrusive.parse($('form'));

before I press the submit button.

在我按下submit按钮之前。

I finally put it in document ready callback.

最后,我把它放入文档准备回调。

$(function () {
    $.validator.unobtrusive.parse($('form'));
});

#3


3  

In your page you have these two line in bottom of the page

在你的页面中有这两行在页面底部。

please See carefully

请仔细看

@section Scripts {
 @Scripts.Render("~/bundles/jqueryval")
}

#4


2  

That's because you doesn't load jquery.validate.min.js before the unobtrusive one.

这是因为您没有加载jquery.validate.min。在不引人注目的之前。

#5


0  

$('#divParent').load("url",
                     function () {
                     $.validator.unobtrusive.parse($('form'));}
                );

Note: This is required if you are loading 'form' or 'PartialView' dynamically inside 'div' using ajax calls

注意:如果您使用ajax调用在“div”中动态加载“form”或“PartialView”,那么这是必需的。

#6


0  

You need just reference these files on every page you need validation my friend!

你需要在每一页上引用这些文件,你需要验证我的朋友!

<script src="~/Scripts/jquery.validate.js"></script>    

#1


22  

Feels like this should work, I think you need to load validate.min.js before validate.unobtrusive.min.js

感觉应该这样,我认为您需要加载确认器。js validate.unobtrusive.min.js之前

#2


10  

I had a similar problem and the only thing that fixed it was to manually call

我有一个类似的问题,唯一的解决办法就是手动调用。

 $.validator.unobtrusive.parse($('form'));

before I press the submit button.

在我按下submit按钮之前。

I finally put it in document ready callback.

最后,我把它放入文档准备回调。

$(function () {
    $.validator.unobtrusive.parse($('form'));
});

#3


3  

In your page you have these two line in bottom of the page

在你的页面中有这两行在页面底部。

please See carefully

请仔细看

@section Scripts {
 @Scripts.Render("~/bundles/jqueryval")
}

#4


2  

That's because you doesn't load jquery.validate.min.js before the unobtrusive one.

这是因为您没有加载jquery.validate.min。在不引人注目的之前。

#5


0  

$('#divParent').load("url",
                     function () {
                     $.validator.unobtrusive.parse($('form'));}
                );

Note: This is required if you are loading 'form' or 'PartialView' dynamically inside 'div' using ajax calls

注意:如果您使用ajax调用在“div”中动态加载“form”或“PartialView”,那么这是必需的。

#6


0  

You need just reference these files on every page you need validation my friend!

你需要在每一页上引用这些文件,你需要验证我的朋友!

<script src="~/Scripts/jquery.validate.js"></script>