简体   繁体   中英

How to send model binded to view from ajax call on button click to controller action?

Here is my ajax call:

$('#addNominationForm').on("submit", function (e) {
    debugger;
    e.preventDefault();
    $.ajax({
        type: 'post',
        url: '@Url.Action("AddNomination", "Nomination")',
        data: JSON.stringify({ model: model, submit: "submit" }),
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            swal({
                title: "Nominated Successfully",
                showCancelButton: false,
                confirmButtonColor: "#88a364",
                confirmButtonText: "Ok"
            },
            function () {
                if (isConfirm) {
                    window.location.href = '@Url.Action("Dashboard", "Dashboard")';
                }
            });
        },
        Error: function (xhr, status, error) {
            alert("error" + xhr.responseText)
        }
    });
});

This is action:

[HttpPost]
[CustomeAuthorize(AllowedRole = "Manager")]
public ActionResult AddNomination(NominationViewModel model, string submit)
{
    //business logic in action
}

But here when ajax call is done, in action model contains null. How can I do it?Or is there any other way to send model on view using ajax call on normal button click?

Dont stringify your data:

data: { model: model, submit: "submit" }

POST has a body that is JSON (in this case)

GET can get data from the querystring so thats where you need stringify

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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