如何发送绑定模型以通过按钮单击控制器操作从ajax调用查看?

时间:2016-06-09 13:34:36

标签: javascript jquery ajax asp.net-mvc-4

这是我的ajax电话:

$('#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)
        }
    });
});

这是行动:

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

但是当ajax调用完成时,在action模型中包含null。我怎么能这样做?或者有没有其他方法在正常按钮点击时使用ajax调用在视图上发送模型?

1 个答案:

答案 0 :(得分:0)

不要将数据字符串化:

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

POST的主体是JSON(在本例中)

GET可以从查询字符串中获取数据,以便您需要stringify