【发布时间】:2015-05-14 04:21:15
【问题描述】:
我正在尝试学习 ASP.Net MVC,我想将 JSON 对象数组发布到服务器并将其发送回客户端。但我得到了奇怪的结果。 Everything was fine without adding the fourth variable (bool Required)。例如,我还尝试在发布 JSON 的 JavaScript 部分中将 true 更改为 "true",但我从服务器得到了同样奇怪的结果。 POST 的结果在使用 JavaScript 和 POSTMAN 时都是一样的,我认为问题出在服务器端代码上。
任何帮助都会很棒。
这是我的控制器代码:
public ActionResult GetResult()
{
List<SurveyDetails> tableList = new List<SurveyDetails>();
tableList.Add(new SurveyDetails { Id = 500, Question = "where are you from", Answer = 2, Required = true });
tableList.Add(new SurveyDetails { Id = 501, Question = "how old are you", Answer = 1, Required = false });
tableList.Add(new SurveyDetails { Id = 502, Question = "what is your first car", Answer = 2, Required = false });
tableList.Add(new SurveyDetails { Id = 503, Question = "do you have kids", Answer = 1, Required = true });
return Json(tableList, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult GetResult(List<Table> list)
{
return Json(list);
}
这是我的对象声明:
namespace CapstoneProject.Models
{
public class SurveyDetails
{
public int Id { get; set; }
public string Question { get; set; }
public int Answer { get; set; }
public bool Required { get; set; }
}
}
这是我从服务器获取的信息(GET 请求):
[
{
"Id": 500,
"Question": "where are you from",
"Answer": 2,
"Required": true
},
{
"Id": 501,
"Question": "how old are you",
"Answer": 1,
"Required": false
},
{
"Id": 502,
"Question": "what is your first car",
"Answer": 2,
"Required": false
},
{
"Id": 503,
"Question": "do you have kids",
"Answer": 1,
"Required": true
}
]
这里是我在 POST GET 请求中收到的内容后从服务器返回的奇怪结果:
[
{
"BackImageUrl": "",
"Caption": "",
"CaptionAlign": 0,
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"SupportsDisabledAttribute": false,
"Rows": [],
"AccessKey": "",
"Attributes": {
"Keys": [],
"Count": 0,
"CssStyle": {
"Keys": [],
"Count": 0,
"Value": null
}
},
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"ControlStyle": {
"BackImageUrl": "",
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"CssClass": "",
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"IsEmpty": true,
"RegisteredCssClass": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"Site": null,
"Container": null
},
"ControlStyleCreated": true,
"CssClass": "",
"Style": {
"Keys": [],
"Count": 0,
"Value": null
},
"Enabled": true,
"EnableTheming": true,
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"HasAttributes": false,
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"SkinID": "",
"TabIndex": 0,
"ToolTip": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"ClientIDMode": 0,
"ClientID": "500",
"ID": "500",
"EnableViewState": true,
"ViewStateMode": 0,
"NamingContainer": null,
"BindingContainer": null,
"DataItemContainer": null,
"DataKeysContainer": null,
"Page": null,
"RenderingCompatibility": {
"Major": 4,
"Minor": 5,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"TemplateControl": null,
"Parent": null,
"TemplateSourceDirectory": "/Survey",
"AppRelativeTemplateSourceDirectory": "~/Survey/",
"Site": null,
"Visible": true,
"UniqueID": "500",
"Controls": [],
"ValidateRequestMode": 0
},
{
"BackImageUrl": "",
"Caption": "",
"CaptionAlign": 0,
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"SupportsDisabledAttribute": false,
"Rows": [],
"AccessKey": "",
"Attributes": {
"Keys": [],
"Count": 0,
"CssStyle": {
"Keys": [],
"Count": 0,
"Value": null
}
},
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"ControlStyle": {
"BackImageUrl": "",
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"CssClass": "",
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"IsEmpty": true,
"RegisteredCssClass": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"Site": null,
"Container": null
},
"ControlStyleCreated": true,
"CssClass": "",
"Style": {
"Keys": [],
"Count": 0,
"Value": null
},
"Enabled": true,
"EnableTheming": true,
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"HasAttributes": false,
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"SkinID": "",
"TabIndex": 0,
"ToolTip": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"ClientIDMode": 0,
"ClientID": "501",
"ID": "501",
"EnableViewState": true,
"ViewStateMode": 0,
"NamingContainer": null,
"BindingContainer": null,
"DataItemContainer": null,
"DataKeysContainer": null,
"Page": null,
"RenderingCompatibility": {
"Major": 4,
"Minor": 5,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"TemplateControl": null,
"Parent": null,
"TemplateSourceDirectory": "/Survey",
"AppRelativeTemplateSourceDirectory": "~/Survey/",
"Site": null,
"Visible": true,
"UniqueID": "501",
"Controls": [],
"ValidateRequestMode": 0
},
{
"BackImageUrl": "",
"Caption": "",
"CaptionAlign": 0,
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"SupportsDisabledAttribute": false,
"Rows": [],
"AccessKey": "",
"Attributes": {
"Keys": [],
"Count": 0,
"CssStyle": {
"Keys": [],
"Count": 0,
"Value": null
}
},
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"ControlStyle": {
"BackImageUrl": "",
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"CssClass": "",
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"IsEmpty": true,
"RegisteredCssClass": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"Site": null,
"Container": null
},
"ControlStyleCreated": true,
"CssClass": "",
"Style": {
"Keys": [],
"Count": 0,
"Value": null
},
"Enabled": true,
"EnableTheming": true,
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"HasAttributes": false,
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"SkinID": "",
"TabIndex": 0,
"ToolTip": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"ClientIDMode": 0,
"ClientID": "502",
"ID": "502",
"EnableViewState": true,
"ViewStateMode": 0,
"NamingContainer": null,
"BindingContainer": null,
"DataItemContainer": null,
"DataKeysContainer": null,
"Page": null,
"RenderingCompatibility": {
"Major": 4,
"Minor": 5,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"TemplateControl": null,
"Parent": null,
"TemplateSourceDirectory": "/Survey",
"AppRelativeTemplateSourceDirectory": "~/Survey/",
"Site": null,
"Visible": true,
"UniqueID": "502",
"Controls": [],
"ValidateRequestMode": 0
},
{
"BackImageUrl": "",
"Caption": "",
"CaptionAlign": 0,
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"SupportsDisabledAttribute": false,
"Rows": [],
"AccessKey": "",
"Attributes": {
"Keys": [],
"Count": 0,
"CssStyle": {
"Keys": [],
"Count": 0,
"Value": null
}
},
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"ControlStyle": {
"BackImageUrl": "",
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"CssClass": "",
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"IsEmpty": true,
"RegisteredCssClass": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"Site": null,
"Container": null
},
"ControlStyleCreated": true,
"CssClass": "",
"Style": {
"Keys": [],
"Count": 0,
"Value": null
},
"Enabled": true,
"EnableTheming": true,
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"HasAttributes": false,
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"SkinID": "",
"TabIndex": 0,
"ToolTip": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"ClientIDMode": 0,
"ClientID": "503",
"ID": "503",
"EnableViewState": true,
"ViewStateMode": 0,
"NamingContainer": null,
"BindingContainer": null,
"DataItemContainer": null,
"DataKeysContainer": null,
"Page": null,
"RenderingCompatibility": {
"Major": 4,
"Minor": 5,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"TemplateControl": null,
"Parent": null,
"TemplateSourceDirectory": "/Survey",
"AppRelativeTemplateSourceDirectory": "~/Survey/",
"Site": null,
"Visible": true,
"UniqueID": "503",
"Controls": [],
"ValidateRequestMode": 0
}
]
【问题讨论】:
-
当你的类被称为
SurveyResult时,为什么你的GetResult()方法采用List<Table>?如果我只是简单地执行Debug.WriteLine(JsonConvert.SerializeObject(new System.Web.UI.WebControls.Table [1] { new System.Web.UI.WebControls.Table() } , Formatting.Indented)),我会看到与您看到的大致相同的 JSON。
标签: javascript c# asp.net json asp.net-mvc