How to call MVC Action using Jquery AJAX and then submit form in MVC?

后端 未结 3 1966
暗喜
暗喜 2020-12-14 19:00

On my MVC View I have button:


When I click this button I need c

相关标签:
3条回答
  • 2020-12-14 19:14

    Use preventDefault() to stop the event of submit button and in ajax call success submit the form using submit():

    $('#btnSave').click(function (e) {
        e.preventDefault(); // <------------------ stop default behaviour of button
        var element = this;    
        $.ajax({
            url: "/Home/SaveDetailedInfo",
            type: "POST",
            data: JSON.stringify({ 'Options': someData}),
            dataType: "json",
            traditional: true,
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                if (data.status == "Success") {
                    alert("Done");
                    $(element).closest("form").submit(); //<------------ submit form
                } else {
                    alert("Error occurs on the Database level!");
                }
            },
            error: function () {
                alert("An error has occured!!!");
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-14 19:19

    Assuming that your button is in a form, you are not preventing the default behaviour of the button click from happening i.e. Your AJAX call is made in addition to the form submission; what you're very likely seeing is one of

    1. the form submission happens faster than the AJAX call returns
    2. the form submission causes the browser to abort the AJAX request and continues with submitting the form.

    So you should prevent the default behaviour of the button click

    $('#btnSave').click(function (e) {
    
        // prevent the default event behaviour    
        e.preventDefault();
    
        $.ajax({
            url: "/Home/SaveDetailedInfo",
            type: "POST",
            data: JSON.stringify({ 'Options': someData}),
            dataType: "json",
            traditional: true,
            contentType: "application/json; charset=utf-8",
            success: function (data) {
    
                // perform your save call here
    
                if (data.status == "Success") {
                    alert("Done");
                } else {
                    alert("Error occurs on the Database level!");
                }
            },
            error: function () {
                alert("An error has occured!!!");
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-14 19:30

    Your C# action "Save" doesn't execute because your AJAX url is pointing to "/Home/SaveDetailedInfo" and not "/Home/Save".

    To call another action from within an action you can maybe try this solution: link

    Here's another better solution : link

    [HttpPost]
    public ActionResult SaveDetailedInfo(Option[] Options)
    {
        return Json(new { status = "Success", message = "Success" });
    }
    
    [HttpPost]
    public ActionResult Save()
    { 
        return RedirectToAction("SaveDetailedInfo", Options);
    }
    

    AJAX:

    Initial ajax call url: "/Home/Save"
    on success callback: 
       make new ajax url: "/Home/SaveDetailedInfo"
    
    0 讨论(0)
提交回复
热议问题