且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

客户端验证在使用 Bootstrap 的 ASP.NET MVC5 中不起作用

更新时间:2022-02-21 16:46:48

我遇到了同样的问题.将生成的 HTML 与一个有效的网站进行比较,我注意到在文档的末尾,有这样的:

I had the same problem. Comparing the generated HTML with a site that worked, I noticed that at the end of the document, there was this:

<script src="/bundles/jqueryval"></script>

...而bundle 应该像这样展开:

...whereas the bundle should have been expanded like this:

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

我查看了 App_Start/BundleConfig.cs,果然,JQuery 验证包没有在 RegisterBundles 中定义.我不得不补充:

I looked in App_Start/BundleConfig.cs, and sure enough, the JQuery validation bundle had not been defined in RegisterBundles. I had to add:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

为什么没有这个,我不知道.我从一个干净的新项目开始.

Why this was missing, I don't know. I started with a new, clean project.

当你引用一个未定义的包时没有编译时错误,这是非常粗制滥造的.即使是永远在线的运行时错误也比静默失败要好.浪费了这么多时间!

It's pretty shoddy that there's no compile-time error when you reference an undefined bundle. Even an always-on run-time error would be better than silent failure. So much time wasted!

原来我的项目中也没有 JQuery 验证脚本.我不得不通过 NuGet(Microsoft JQuery Unobtrusive Validation")添加它们.

turns out I didn't have the JQuery validation scripts in my project, either. I had to add them via NuGet ("Microsoft JQuery Unobtrusive Validation").