MVC 将 json 数据传递给控制器
MVC pass json data to controller
编辑:感谢 Ash 发现这些错误
这里有 3 个问题
1. The JSON request was too large to be deserialized I had to add <add key="aspnet:MaxJsonDeserializerMembers" value="150000" /> in the app settings
2. I forgot the getters and setters in one of my classes
3. I misspelled one of my properties
我正在尝试将一些 json 数据传递给控制器。每当我将 js Object
传递给 Controller
时,我的模型都是空的。默认模型活页夹不应该处理这个吗?我无法找出为什么在将数据传递给控制器时我会得到空值。我看过其他 SO 问题,但 none 到目前为止有帮助。
数据是这样的
{
Data: [{
duration: 5,
end_date: "06-04-2013 00:00"
id: 1,
open: true,
parent: 0,
progress: 0,
start_date: "01-04-2013 00:00",
text : "PcB ddCcgoMordiF Arr e"
}]
Links: //empty array of something similar
}
这些是我的 DTO 的样子
public class GanttRequestDto
{
public IEnumerable<GanttTaskDto> Data;
public IEnumerable<GanttLinkDto> Links;
}
public class GanttTaskDto
{
public int Id { get; set; }
public string Test { get; set; }
public DateTime Start_date { get; set; }
public DateTime End_date { get; set; }
public int Duration { get; set; }
public bool Open { get; set; }
public decimal Progress { get; set; }
public int? ParentId { get; set; }
}
public class GanttLinkDto
{
public int Id { get; set; }
public string Type { get; set; }
public int Source { get; set; }
public int Target { get; set; }
}
我的控制器看起来像这样
[HttpPost]
public BetterJsonResult SaveGanttChartData(GanttRequestDto ganttDataModel)
{
//do something
return null;
}
我的JS代码
InitSaveButton() {
$("#save-btn").click(function() {
var ganttData = gantt.serialize();
var model = {
Data: ganttData.data,
Links: ganttData.links
};
Ajax.ajaxRequest(null, "/Gantt/SaveGanttChartData?ganttDataModel", model, null, "Saving Gantt Data", "Success", null);
});
}
这是我的 Ajax 请求的样子
//url:string
//model:json object
//updateId (optional): area to update,
//toastMessage: optional toast message
//toasTitle: optional toast title
//onComplete: optional callback function
Ajax.ajaxRequest = function (httpVerb, url, model, updateId, toastMessage, toastTitle, onComplete) {
if (httpVerb === null || httpVerb.length === 0) httpVerb = "POST";
$.ajax({
url: url,
type: httpVerb,
cache: false,
data: JSON.stringify(model),
dataType: "json",
contentType: 'application/json; charset=utf-8'
}).done(function (data) {
Ajax.ajaxSuccess(data, updateId, toastMessage, toastTitle);
}).fail(function (err) {
Ajax.ajaxFailure(err);
}).always(function (data) {
if (onComplete && typeof onComplete === 'function') {
onComplete(data);
}
});
};
这是您的问题的解决方法。
这里有两件事:
我假设您提供的 JSON 示例数据是纯手工定制的,但仍然提到数据有一些错别字。
var data = {
Data: [{
duration: 5,
end_date: "06-04-2013 00:00",
id: 1,
open: true,
parentId: 0,
progress: 0,
start_date: "01-04-2013 00:00",
test : "PcB ddCcgoMordiF Arr e"
}],
Links: [{Id : 1, Type: "sdsd"}]
}
您没有将数据成员标记为具有 getter 和 setter 的属性。
public class GanttRequestDto
{
public IEnumerable<GanttTaskDto> Data { get; set; }
public IEnumerable<GanttLinkDto> Links { get; set; }
}
现在就试试吧,让我知道它是否适合你。
编辑:感谢 Ash 发现这些错误 这里有 3 个问题
1. The JSON request was too large to be deserialized I had to add <add key="aspnet:MaxJsonDeserializerMembers" value="150000" /> in the app settings
2. I forgot the getters and setters in one of my classes
3. I misspelled one of my properties
我正在尝试将一些 json 数据传递给控制器。每当我将 js Object
传递给 Controller
时,我的模型都是空的。默认模型活页夹不应该处理这个吗?我无法找出为什么在将数据传递给控制器时我会得到空值。我看过其他 SO 问题,但 none 到目前为止有帮助。
数据是这样的
{
Data: [{
duration: 5,
end_date: "06-04-2013 00:00"
id: 1,
open: true,
parent: 0,
progress: 0,
start_date: "01-04-2013 00:00",
text : "PcB ddCcgoMordiF Arr e"
}]
Links: //empty array of something similar
}
这些是我的 DTO 的样子
public class GanttRequestDto
{
public IEnumerable<GanttTaskDto> Data;
public IEnumerable<GanttLinkDto> Links;
}
public class GanttTaskDto
{
public int Id { get; set; }
public string Test { get; set; }
public DateTime Start_date { get; set; }
public DateTime End_date { get; set; }
public int Duration { get; set; }
public bool Open { get; set; }
public decimal Progress { get; set; }
public int? ParentId { get; set; }
}
public class GanttLinkDto
{
public int Id { get; set; }
public string Type { get; set; }
public int Source { get; set; }
public int Target { get; set; }
}
我的控制器看起来像这样
[HttpPost]
public BetterJsonResult SaveGanttChartData(GanttRequestDto ganttDataModel)
{
//do something
return null;
}
我的JS代码
InitSaveButton() {
$("#save-btn").click(function() {
var ganttData = gantt.serialize();
var model = {
Data: ganttData.data,
Links: ganttData.links
};
Ajax.ajaxRequest(null, "/Gantt/SaveGanttChartData?ganttDataModel", model, null, "Saving Gantt Data", "Success", null);
});
}
这是我的 Ajax 请求的样子
//url:string
//model:json object
//updateId (optional): area to update,
//toastMessage: optional toast message
//toasTitle: optional toast title
//onComplete: optional callback function
Ajax.ajaxRequest = function (httpVerb, url, model, updateId, toastMessage, toastTitle, onComplete) {
if (httpVerb === null || httpVerb.length === 0) httpVerb = "POST";
$.ajax({
url: url,
type: httpVerb,
cache: false,
data: JSON.stringify(model),
dataType: "json",
contentType: 'application/json; charset=utf-8'
}).done(function (data) {
Ajax.ajaxSuccess(data, updateId, toastMessage, toastTitle);
}).fail(function (err) {
Ajax.ajaxFailure(err);
}).always(function (data) {
if (onComplete && typeof onComplete === 'function') {
onComplete(data);
}
});
};
这是您的问题的解决方法。
这里有两件事:
我假设您提供的 JSON 示例数据是纯手工定制的,但仍然提到数据有一些错别字。
var data = { Data: [{ duration: 5, end_date: "06-04-2013 00:00", id: 1, open: true, parentId: 0, progress: 0, start_date: "01-04-2013 00:00", test : "PcB ddCcgoMordiF Arr e" }], Links: [{Id : 1, Type: "sdsd"}] }
您没有将数据成员标记为具有 getter 和 setter 的属性。
public class GanttRequestDto { public IEnumerable<GanttTaskDto> Data { get; set; } public IEnumerable<GanttLinkDto> Links { get; set; } }
现在就试试吧,让我知道它是否适合你。