C# 从json post请求接收mvc5控制器中的值

C# 从json post请求接收mvc5控制器中的值,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,请检查下面的代码。我得到了一些订单ID和一个状态为string的传统值。但在我的mvc5控制器上,该值未被接收。控制器正在命中,但模型值为空。我在这里干什么?我需要以其他方式更改模型吗 C#模型: 我将发送: [{"Status":"Pending"},{"OrderIds":"9"},{"OrderIds":"3"}] Jquery: $(document.body).on("click", "#btnConfirm", function () { var Order

请检查下面的代码。我得到了一些订单ID和一个状态为string的传统值。但在我的mvc5控制器上,该值未被接收。控制器正在命中,但模型值为空。我在这里干什么?我需要以其他方式更改模型吗

C#模型:

我将发送:

[{"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);
                }
            });


        });
您可以尝试以下方法:

根据您的模型类生成对象,并像调用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);
                }
            });


        });

尝试发送
var things=JSON.stringify({Status:“value1”,OrderIds:[1,2,3]})
在您发送的JSON中,状态和orderID不属于同一个对象。Try:
[{Status:“Pending”,OrderIds:[“9”,“3”]}]
var-oRequest={};oRequest[“状态”]=订单状态。var allSelectedProductIdWithKey=[]$('.chkItems:checked').each(函数(){allSelectedProductIdWithKey.push({$(this.val()});});oRequest[“OrderID”]=所有SelectedProductIDWithKey-请在代码中尝试此修改后的代码段
$(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);
                }
            });


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


        });
        [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);
            }
        }