如何使用 asp.net mvc 3 jquery 验证与 ajax 提交的 jquery 对话框?
我正在使用
asp.net mvc 3 jquery 验证 无阻碍的 JavaScript。
我试图通过注释在服务器端编写所有验证,然后让 mvc 3 的新功能负责客户端。
我有一个对话框,上面有一个按钮(只是一个按钮,不是提交按钮),我想通过 ajax 将数据发布到服务器。
因此,当用户单击按钮时,我会提交表单并返回 false 以取消回发。
我认为这会触发验证,但情况似乎并非如此。如何触发客户端验证?
编辑
<form method="post" id="TaskFrm" action="/Controller/Action">
<input type="text" value="" name="Name" id="Name" data-val-required="Name field cannot be left blank" data-val-length-max="100" data-val-length="task cannot exceed 100 characters" data-val="true">
</form>
var $dialog = $('<div></div>').dialog(
{
width: 580,
height: 410,
resizable: false,
modal: true,
autoOpen: false,
title: 'Basic Dialog',
buttons:
{
Cancel: function ()
{
$(this).dialog('close');
},
'Create Task': function ()
{
var createSubmitFrmHandler = $(my.selectors.createFrm).live('submit', function ()
{
alert('hi');
return false;
});
createSubmitFrmHandler .validate();
var a = createSubmitFrmHandler .valid();
alert(a);
}
}
});
这始终返回 true。
编辑2
如果我在表单中放置一个提交按钮,它将显示客户端验证(我使用jquery返回 false,如我的代码所示)。
所以这意味着我确实拥有脚本和所有内容,但当我尝试以编程方式进行操作时,由于未知原因它无法工作。
编辑3
我卡住了jquery validate &&母版页中的 jquery.validate.unobtrusive 文件。但是,当我将它们粘贴在包含加载字段的部分视图中,然后强制提交时,验证就会启动。
我不明白。我非常确定路径是正确的,因为我刚刚将文件拖放到我的母版页中,它就找出了路径。将其放在部分视图中并不是真正的解决方案,因为我必须多次执行此操作,这意味着每次加载部分视图时我都会获得这些文件的另一个副本。
编辑4
我认为这只是 jquery.validate.unobtrusive 出于某种原因每次都需要加载。我不知道为什么。
I am using
asp.net mvc 3
jquery validate
unobstructive javascript.
I am trying to write all my validation on the serverside through annotations and then have the new feature of mvc 3 take care of the client side.
I have a dialog that has a button on it(just a button not a submit button) that I want to post data to the server through ajax.
So when the user clicks on the button I do a form submit and return false to cancel the post back.
I thought that would trigger the validation but that does not seem to be the case. How do I make the client side validation trigger?
Edit
<form method="post" id="TaskFrm" action="/Controller/Action">
<input type="text" value="" name="Name" id="Name" data-val-required="Name field cannot be left blank" data-val-length-max="100" data-val-length="task cannot exceed 100 characters" data-val="true">
</form>
var $dialog = $('<div></div>').dialog(
{
width: 580,
height: 410,
resizable: false,
modal: true,
autoOpen: false,
title: 'Basic Dialog',
buttons:
{
Cancel: function ()
{
$(this).dialog('close');
},
'Create Task': function ()
{
var createSubmitFrmHandler = $(my.selectors.createFrm).live('submit', function ()
{
alert('hi');
return false;
});
createSubmitFrmHandler .validate();
var a = createSubmitFrmHandler .valid();
alert(a);
}
}
});
This always return true.
Edit 2
if I put a submit button within the form it will show the clientside validation(I use jquery to return false as shown in my code).
So this means I do have the scripts and everything but it is not working for unknown reasons when I try to do it programmatic.
Edit 3
I stuck the jquery validate && jquery.validate.unobtrusive files in the master page. But when I stick them in the partial view that contains the fields that get loaded up and then force a submit the validation kicks in.
I don't understand. I pretty sure the pathing is right as I just dragged and dropped the file into my master page and it figured out the path. Having it in the partial views is not really a solution as I going have to do this multiple times and that means every time the partial view loads up I got another copy of these files.
Edit 4
I think it is just the jquery.validate.unobtrusive that needs to be for some reason loaded every time. I am not sure why though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的事情应该可以解决问题:
Something like this should do the trick:
该问题可能是由于在最初加载不显眼的脚本后将元素添加到 DOM 造成的。您可以在创建对话框后调用
$.validator.unobtrusive.parse("form")
强制其将验证处理程序附加到新元素。尽管我们在对话框中显示表单,但这解决了我们的类似问题。
The issue may be due to elements being added to the DOM after the unobtrusive script loads initially. You can force it to attach validation handlers the new elements by calling
$.validator.unobtrusive.parse("form")
after creating the dialog.This solved our similar issue although we display the form within the dialog.
这可以使用 ASP.NET MVC 中名为“远程验证”的功能来完成。您所要做的就是使用
[Remote]
属性来装饰您想要验证的属性,该属性传递执行验证的方法的名称以及该属性所在的控制器。你可以找到一个例子来说明如何
如何:在 ASP.NET MVC 中实现远程验证
This can be done using a feature of ASP.NET MVC called
Remote Validation
. All you have to do is decorate the property you want validated with a[Remote]
attribute passing the name of the method that does the validation and the controller on which it exists.you can find an example on how
How to: Implement Remote Validation in ASP.NET MVC