从 json post 请求接收 mvc5 控制器中的值
receive value in mvc5 controller from json post request
请检查下面的代码。我正在收集一些订单 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
}
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);
}
});
});
你可以试试这个:
根据您的模型制作对象 class 并像调用 Ajax 一样进行字符串化 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);
}
}
请检查下面的代码。我正在收集一些订单 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
}
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);
}
});
});
你可以试试这个:
根据您的模型制作对象 class 并像调用 Ajax 一样进行字符串化 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);
}
}