客户端验证在使用 Bootstrap 的 ASP.NET MVC5 中不起作用 [英] Client-side validation not working in ASP.NET MVC5 with Bootstrap

查看:30
本文介绍了客户端验证在使用 Bootstrap 的 ASP.NET MVC5 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 Visual Studio 2013,我使用 Bootstrap 创建了一个新的 ASP.NET MVC (5) 项目.

Running Visual Studio 2013, I created a new ASP.NET MVC (5) project, with Bootstrap.

但是,我无法让不显眼的客户端验证工作.

However, I'm unable to get unobtrusive client-side validation working.

该模型在相关属性上具有 [Required] 属性,并且视图具有表单中每个字段的 ValidationMessageFor... 标记.

The model has [Required] attributes on the relevant properties and the view has ValidationMessageFor... tags for each field in the form.

但是,提交表单会导致表单在验证消息出现之前回发到服务器.

However, submitting the form causes the form to postback to server before validation messages appear.

使用 NuGet,我安装了 jquery.validatejquery.validate.unobtrusive 并将它们添加到 App_Start 中的 jquery 包中.

Using NuGet, I've installed jquery.validate and jquery.validate.unobtrusive and added them to the jquery bundle in App_Start.

然而它继续顽固地回发到服务器.F12 开发工具未显示任何 JS 错误/警告.

Yet it continues to stubbornly post back to server. F12 dev tools shows no JS errors/warnings.

有没有其他人遇到过这个问题(在 SO 中看不到任何与 MVC 5 相关的具体内容),您有什么想法吗?

Has anybody else come across this issue (can't see anything in SO relating to MVC 5 specifically) and do you have any ideas?

推荐答案

我遇到了同样的问题.将生成的 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").

这篇关于客户端验证在使用 Bootstrap 的 ASP.NET MVC5 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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