如何将复杂的键值参数从视图传递给mvc中的控制器?
How to pass complex key-value parameters form view to controller in mvc?
我在将复杂的键值对从视图发送到控制器时遇到了 ajax 的问题。我写了 ajax 个查询。除了 "currentStateDatas" 所有其他变量都正确传递给控制器。但是,currentStateDatas 从视图中变为 null。控制器等待 "string" "currentStateDatas" 因为我需要它的字符串类型。我无法弄清楚问题所在。请你帮助我好吗 ?顺便说一句 "currentStateDatas" 包含 json。它有太多嵌套的键值对。
currentStateDatas = {};
//some values are taken from forms.
$.ajax({
url: "@Url.Action("SaveTempReport", "Report")",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "categoryIds": categoryIds, "reportName": reportName, "Description": description, "tempReportId": tempReportId, "chartState": currentStateDatas, "deparmentIds": deparmentIds}),
success: function (response) {
}
});
您的控制器等待 currentStateDatas
作为 string
,但在您的 js 代码中它是一个对象,您还应该使用 JSON.stringify
将 currentStateDatas
转换为字符串。
$.ajax({
url: "@Url.Action("SaveTempReport", "Report")",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "categoryIds": categoryIds,
"reportName": reportName,
"Description": description,
"tempReportId": tempReportId,
"chartState": JSON.stringify(currentStateDatas),
"deparmentIds": deparmentIds}),
success: function (response) {
}
});
我在将复杂的键值对从视图发送到控制器时遇到了 ajax 的问题。我写了 ajax 个查询。除了 "currentStateDatas" 所有其他变量都正确传递给控制器。但是,currentStateDatas 从视图中变为 null。控制器等待 "string" "currentStateDatas" 因为我需要它的字符串类型。我无法弄清楚问题所在。请你帮助我好吗 ?顺便说一句 "currentStateDatas" 包含 json。它有太多嵌套的键值对。
currentStateDatas = {};
//some values are taken from forms.
$.ajax({
url: "@Url.Action("SaveTempReport", "Report")",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "categoryIds": categoryIds, "reportName": reportName, "Description": description, "tempReportId": tempReportId, "chartState": currentStateDatas, "deparmentIds": deparmentIds}),
success: function (response) {
}
});
您的控制器等待 currentStateDatas
作为 string
,但在您的 js 代码中它是一个对象,您还应该使用 JSON.stringify
将 currentStateDatas
转换为字符串。
$.ajax({
url: "@Url.Action("SaveTempReport", "Report")",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "categoryIds": categoryIds,
"reportName": reportName,
"Description": description,
"tempReportId": tempReportId,
"chartState": JSON.stringify(currentStateDatas),
"deparmentIds": deparmentIds}),
success: function (response) {
}
});