C# MVC 4 客户端验证不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16811816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:50:30  来源:igfitidea点击:

MVC 4 Client side validation not working

c#asp.net-mvc-4razor

提问by Larry

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)

我的两个 web.config 文件中都有这个(1 是全局的,1 是在视图文件夹中)

<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.

不想在这里发帖,我在这里阅读了一些相关的主题,但似乎无法解决我的问题,当我点击提交时,它正在发出帖子/获取请求。

采纳答案by Adil

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

感觉这应该可行,我认为您需要在 validate.unobtrusive.min.js 之前加载 validate.min.js

回答by Amin Saqi

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

那是因为您没有在不显眼的之前加载 jquery.validate.min.js。

回答by J.B.Vala

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

在您的页面中,您在页面底部有这两行

please See carefully

请仔细看

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

回答by Gudradain

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.

在我按下提交按钮之前。

I finally put it in document ready callback.

我终于把它放在文档就绪回调中。

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

回答by Jagdeesh Motegowda

$('#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”,则这是必需的

回答by S.Mohamed Mahdi Ahmadian zadeh

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

您只需在需要验证的每个页面上引用这些文件,我的朋友!

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