从json发布请求中在mvc5控制器中接收值

时间:2019-10-10 04:36:05

标签: c# asp.net-mvc-5

请检查下面的代码。我正在收集一些订单ID,其状态的附加值为字符串。但是在我的mvc5控制器上,此值未收到。控制器命中,但模型值为空。我在这里做什么错?我需要以其他方式更改模型吗?

C#模型:

 public class ChangeOrderStatus
    {
        public string Status { get; set; }
        public List<int> OrderIds { get; set; }
    }

mvc5控制器:

[HttpPost]
public ActionResult ChangeStatus(ChangeOrderStatus ChangeOrderStatus)
{
  //this is the post controller not hitting on that json 
}

我要发送的杰森:

[{"Status":"Pending"},{"OrderIds":"9"},{"OrderIds":"3"}]

jQuery:

$(document.body).on("click", "#btnConfirm", function () {
            var OrderStatus = $("#OrderStatus").val();
            //console.log(OrderStatus);

            var allSelectedProductIdWithKey = [];
            allSelectedProductIdWithKey.push({ Status: OrderStatus });
            $('.chkItems:checked').each(function () {
                allSelectedProductIdWithKey.push({ OrderIds: $(this).val() });
            });
            var things = JSON.stringify(allSelectedProductIdWithKey);

            console.log(things);//out from this looks like this: [{"Status":"Pending"},{"OrderIds":"9"},{"OrderIds":"3"}]

            $.ajax({
                url: '/Orders/ChangeStatus',
                type: "POST",
                contentType: "application/json",
                dataType: "json",
                data: things,

                success: function (result) {
                    if (result == 'ok') {
                        alert("Success! Order created");
                        location.reload();
                    } else {
                        alert("Error! Order not created. Something wrong");
                    }
                },

                error: function (xhr, resp, text) {
                    console.log(xhr, resp, text);
                }
            });


        });

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

根据您的Model类制作对象,并像调用Ajax一样进行字符串化。

$("#btnConfirm").on("click", function () {
            var OrderStatus = $("#OrderStatus").val();
            //console.log(OrderStatus);
            var allSelectedProductIdWithKey = [];

            var arr = [10, 20, 30, 40, 50];



            for (let i = 0; i < arr.length; ++i) {
                allSelectedProductIdWithKey.push(arr[i]);
            }


            //allSelectedProductIdWithKey.push({ Status: OrderStatus });
            //$('.chkItems:checked').each(function () {
            //    allSelectedProductIdWithKey.push({ OrderIds: $(this).val() });
            //});


            //var things = JSON.stringify(allSelectedProductIdWithKey);

            var things = {
                Status: OrderStatus,
                OrderIds: allSelectedProductIdWithKey
            };



            $.ajax({
                url: '/Orders/ChangeStatus',
                type: "POST",
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(things),

                success: function (result) {
                    if (result == 'ok') {
                        alert("Success! Order created");
                        location.reload();
                    } else {
                        alert("Error! Order not created. Something wrong");
                    }
                },

                error: function (xhr, resp, text) {
                    console.log(xhr, resp, text);
                }
            });


        });

请根据需要对其进行修改...

在Controller中,最好将JsonResult发送回:

        [HttpPost]
        public JsonResult ChangeStatus(ChangeOrderStatus ChangeOrderStatus)
        {
            var result = "NOK";

            try
            {
                // ..............  Your code here

                result = "ok";
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }