简体   繁体   English

如何将绑定到视图的模型从Ajax调用中单击到控制器动作?

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

Here is my ajax call: 这是我的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)
        }
    });
});

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. 但是在这里,当ajax调用完成时,实际模型中将包含null。 How can I do it?Or is there any other way to send model on view using ajax call on normal button click? 我该怎么办?或者还有其他方法可以通过在普通按钮单击时使用ajax调用在视图上发送模型吗?

Dont stringify your data: 不要对您的数据进行分类:

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

POST has a body that is JSON (in this case) POST的主体为JSON(在这种情况下)

GET can get data from the querystring so thats where you need stringify GET可以从查询字符串中获取数据,因此您需要在其中进行字符串化

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

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