简体   繁体   English

如何通过Ajax提交ASP NET MVC3表单,同时覆盖默认行为,同时保持客户端验证

[英]How to submit ASP NET MVC3 Form through ajax while overriding default behavior while maintaining client side validation

I want to be able to post this form data using an ajax post, but I cannot figure out how to override the default behavior when the form is posted. 我希望能够使用ajax发布来发布此表单数据,但是我无法弄清楚在发布表单时如何覆盖默认行为。 I still want client side validation from ASP NET MVC3 though. 我仍然希望从ASP NET MVC3进行客户端验证。 For example, when I click "submit" in this form, it goes directly to the controller action when I have a jquery function that is supposed to directly handle any submit from this form. 例如,当我单击此表单中的“提交”时,当我有一个应该直接处理此表单中的任何提交的jquery函数时,它将直接进入控制器操作。

 @model BlogApp.Models.PostUser

 @using (Html.BeginForm("LoginForm", "Home", FormMethod.Post, new { id = "testform" }))
{
@Html.ValidationSummary(true)
<fieldset>
    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name, new { id = "Name" })
        @Html.ValidationMessageFor(model => model.Name)
    </div>
        <input type="submit" value="submit" />
</fieldset>
}

this is the jquery function: 这是jquery函数:

 $('#testform').live('submit', function () {
    $('#testform').validate();

    var name = $('#Name').val();
    var logout = '<a href="#" onClick="logOut()">logout</a>';
    var login_success = '<p>Logged in as: ' + name + '</p>' + logout;

    if ($(this).valid()) {
        $.ajax({
            url: '/home/loginform',
            type: 'POST',
            data: { name: name },
            success: function (data) {
                if (data === true) {
                    $('#loginform').empty();
                    $('#loginform').append(login_success);
                }
                else {
                    alert('Failed to save the user');
                }
            }
        });
    }


}); 

So my question is: instead of the form automatically going to the controller action (/Home/LoginForm/) and returning that result, how can I make submitting the form fire my jquery function (so that the page doesn't auto-refresh), but still maintaining the client side validation? 所以我的问题是:代替表单自动进入控制器动作(/ Home / LoginForm /)并返回该结果,我如何使表单触发我的jquery函数(以便页面不会自动刷新) ,但仍保持客户端验证?

The <input type="submit" value="submit" /> is probably the culprit. <input type="submit" value="submit" />可能是罪魁祸首。 The default form submission is occurring because of the input type submit. 由于输入类型为Submit,因此发生了默认表单提交。 You could change your submit button to be a different html tag (like a span) and then use JQuery to assign the click event to handle your submission, or you could cancel the default action within your submit handler you have now (see the .submit() documentation for JQuery on how to do that). 您可以将Submit按钮更改为其他html标签(例如span),然后使用JQuery分配click事件来处理提交,或者您可以取消现有的Submit处理程序中的默认操作(请参阅.submit ()有关如何执行此操作的JQuery文档 )。

...we can cancel the submit action by calling .preventDefault() on the event object or by returning false from our handler ...我们可以通过在事件对象上调用.preventDefault()或从处理程序中返回false来取消提交操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM