简体   繁体   English

取消提交MVC表格

[英]Cancel MVC Form submit

I am submitting a form using AJAX. 我正在使用AJAX提交表单。 The fields are in a partial view that get sent to a controller action. 这些字段在局部视图中发送给控制器操作。 The partial view is in a div that pops up when a button on the main form is clicked. 单击主窗体上的按钮时,局部视图位于div中。 If the user completes the form and clicks the submit button it then the update is performed in the controller action and all is well. 如果用户填写表单并单击“提交”按钮,则将在控制器操作中执行更新,一切正常。 However, if the user changes their mind I like to add a cancel button to hide the form. 但是,如果用户改变主意,我想添加一个取消按钮来隐藏表单。 Adding the button and hiding the form works as expected, but the call to the controller action method still occurs. 添加按钮并隐藏表单可以按预期工作,但是仍然会发生对控制器action方法的调用。 I've tried using 我试过使用

e.preventDefault();

but this hasn't worked. 但这没有用。

I've tried using a second JQuery method attached to the cancel id but I can't get this to work. 我尝试使用附加到取消ID的第二个JQuery方法,但无法正常工作。

My JQuery looks like this: 我的JQuery看起来像这样:

$('#VisitorModal').on('submit', '#visitorform', function (e) {
    var data = $(this).serialize();
    var url = '@Url.Action("CreateVisitor", "Meetings")';
    var text = $('#title').val() + ' ' + $('#firstname').val() + ' ' + $('#surname').val() + ' (' + $('#company').val() + ')'; // a value from the form that you want as the option text
    $.post(url, data, function (response) {
        if (response) {
            $('#VisitorID').append($('<option></option>').val(response).text(text)).val(response);
        } else {
            dialog.hide();
        }
    }).fail(function () {
        dialog.hide();
    });
    dialog.hide();
    e.preventDefault();
    //return false; // cancel the default submit
});

$('#cancel').on('click', function () {
    dialog.hide();
});

And here's my action method: 这是我的操作方法:

[HttpPost]
public JsonResult CreateVisitor(AppointmentViewModel model)
{
    var visitor = new Visitor()
    {
        Title = model.VisitorTitle,
        FirstName = model.VisitorFirstName,
        LastName = model.VisitorSurname,
        Company = model.VisitorCompany
    };
    db.Visitors.Add(visitor);
    db.SaveChanges();
    return Json(visitor.id);
}

And submit & cancel buttons here: 并在此处提交和取消按钮:

<div class="form-group">
    <div class="col-md-offset-2 col-md-5">
        <input type="submit" value="Create" class="btn btn-success" id="submit" />
        <input type="button" value="Cancel" class="btn btn-warning" id="cancel" />
    </div>
</div>

Does anyone have any suggestions that might get this to work as I hope? 有没有人有什么建议可以像我希望的那样起作用?

Since your form is being loaded dynamically after the initial page has been rendered, you need to use event delegation by using .on() to add a listener to an ancestor that exists when the page is rendered 由于在呈现初始页面之后会动态加载表单,因此您需要使用事件委托 ,方法是使用.on()将侦听器添加到呈现页面时存在的祖先中

Instead of 代替

$('#cancel').on('click', function () {
    dialog.hide();
});

use 采用

$(document).on('click', '#cancel', function () {
    dialog.hide();
});

but replace document with the closest ancestor that exists when the page is first generated. 但是将document替换为首次生成页面时存在的最接近的祖先。

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

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