开发者

Why my MVC 3 form post refreshes the page after submit?

开发者 https://www.devze.com 2023-04-04 19:38 出处:网络
I have this form: @using (Html.BeginForm(\"Update\", \"MyController\", FormMethod.Post, new { id = \"frmEdit\" })) {

I have this form:

@using (Html.BeginForm("Update", "MyController", FormMethod.Post, new { id = "frmEdit" })) {
<input type="text" id="Title" value="Adam" />
}

Here is the jquery:

  $(function () {
        $('#frmEdit').submit(function () {
            //read form values            
            //verify req fields
            if (isValid) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    beforeSend: SpinWheel(l),
                    success: function (result) {
                        if (result.s == 'OK') {
                            alert("Success Updated");                                                    
                        }
                        else {
                            Alert("Sorry! Update failed.");
                        }
                    },
                    error: function (xhr, status, error) { alert('Error');}
                });
            }
            return false;
        });

    });    

Controller:

public ActionResult Update(FormCollection album)
{
   return Json(new { s = "OK" });
}

The form has a modal inside it. When I click a link in parent page, the modal is launched. In the modal I have the form with a submit butto开发者_如何学运维n.


Try adding a call to preventDefault() on the event passed in:

$('#frmEdit').submit(function (e) {
    e.preventDefault();

    ....
});

bad copy/paste? alert(Error');

If so, this could be the reason it wasn't hitting the return false; at the end of your submit event

0

精彩评论

暂无评论...
验证码 换一张
取 消