使用 jQuery 验证 ASP.NET MVC 3.0 AjaxForm 时出现错误

发布于 2024-11-11 01:15:54 字数 944 浏览 0 评论 0原文

尝试使用此解决方案使用 jQuery 验证 AjaxForm ASP.Net MVC Ajax 表单验证,但我收到错误:未捕获的异常:TypeError:无法将 '$('MyFrom').validate()' 转换为对象

我的来源:

@using (Ajax.BeginForm("MyFromAction", "Something", null, new AjaxOptions() { OnBegin = "onBeginMyFrom", OnFailure = "onFailureMyFrom", OnSuccess = "onSuccessMyFrom" }, new { @id = "MyFrom" }))
{
}

我的表单是工作正常并发布到服务器,但在我使用 jQuery 验证后,我收到上面的错误。

有什么想法吗?

更新: 我没有提到我正在使用 MVCContrib FluentHtml。

MVCContrib 需要这个 http://weblogs.asp.net/srkirkland/archive/2011/03/08/adding-unobtrusive-validation-to-mvccontrib-fluence-html.aspx 以获得不显眼的验证工作。

MvcContrib 是否会在下一个版本中默认支持 Unobtrusive 验证?

Trying to use this solution to validate AjaxForm ASP.Net MVC Ajax form with jQuery validation but i am getting error: Uncaught exception: TypeError: Cannot convert '$('MyFrom').validate()' to object

My from:

@using (Ajax.BeginForm("MyFromAction", "Something", null, new AjaxOptions() { OnBegin = "onBeginMyFrom", OnFailure = "onFailureMyFrom", OnSuccess = "onSuccessMyFrom" }, new { @id = "MyFrom" }))
{
}

My form was working correctly and posting to the server, but after i use jQuery validation i am getting error above.

Any ideas?

UPDATE:
I didn't mention that i am using MVCContrib FluentHtml.

MVCContrib required this http://weblogs.asp.net/srkirkland/archive/2011/03/08/adding-unobtrusive-validation-to-mvccontrib-fluent-html.aspx to get Unobtrusive validation work.

Is MvcContrib will support Unobtrusive validation in the next release by default?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-11-18 01:15:54

您正在查看的答案与 ASP.NET MVC 的早期版本有关。在 ASP.NET MVC 3 中,客户端验证是使用 jquery 验证插件执行的,并且是在不显眼的情况下完成的。您不需要任何代码。因此,第一件事是确保您已包含正确的脚本:

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

然后在 web.config 中启用不显眼的 javascript 和验证:

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

现在您可以让 AJAX 表单正常运行:

@using (Ajax.BeginForm("MyFromAction", "Competition", null, new AjaxOptions() { OnBegin = "onBeginMyFrom", OnFailure = "onFailureMyFrom", OnSuccess = "onSuccessMyFrom" }, new { @id = "MyFrom" }))
{
    ...
}

表单将在客户端检查验证错误并且在修复之前不会提交。

The answer you are looking at was related to a previous version of ASP.NET MVC. In ASP.NET MVC 3 client validation is performed using the jquery validate plugin and it is done in an unobtrusive matter. You don't need any code for this. So the first thing is to make sure that you have included the proper scripts:

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

then that unobtrusive javascript and validation are enabled in web.config:

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

Now you can have your AJAX form behave properly:

@using (Ajax.BeginForm("MyFromAction", "Competition", null, new AjaxOptions() { OnBegin = "onBeginMyFrom", OnFailure = "onFailureMyFrom", OnSuccess = "onSuccessMyFrom" }, new { @id = "MyFrom" }))
{
    ...
}

The form will check for validation errors at the client side and it will not be submitted until they are fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文