如何使用asp.net MVC 3 JQuery验证与jQuery的对话框,它一个ajax提交? [英] How to use asp.net mvc 3 jquery validate with a jquery dialog that does an ajax submit?
问题描述
我使用
asp.net MVC 3
JQuery验证
无碍的javascript。
asp.net mvc 3 jquery validate unobstructive javascript.
我想通过注释写我在服务器端的所有验证,然后让MVC 3照顾客户端的新功能。
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.
我有我想通过AJAX发布数据到服务器上有一个按钮(只是一个按钮而不是提交按钮)对话框。
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.
因此,当在用户点击按钮我做一个表单提交并返回false就取消后回来。
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?
修改
<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如图我code)。
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.
修改3
我坚持了JQuery验证和放大器;&安培;在母版页jquery.validate.unobtrusive文件。但是,当我坚持他们包含得到加载起来,然后将字段强制在提交验证踢。局部视图
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.
我不明白。我pretty确保路径是正确的,因为我刚才拖动,文件投进我的母版页,并计算出的路径。有它在部分意见是不是真的,因为我打算要这个多次做一个解决方案,这意味着每一次局部视图负荷高达我得到了这些文件的另一个副本。
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.
修改4
我觉得这只是一个需要加载为每次某种原因jquery.validate.unobtrusive。我不知道为什么,虽然。
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.
推荐答案
这样的事情应该做的伎俩:
Something like this should do the trick:
$("#my-button").click(function (event) {
event.preventDefault();
$("#my-form").validate();
if ($("#my-form").valid()) {
// ...
}
});
这篇关于如何使用asp.net MVC 3 JQuery验证与jQuery的对话框,它一个ajax提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!