我正在尝试学习 ASP.Net MVC,我想将 JSON 对象数组发布到服务器并将其发送回客户端。但我得到了奇怪的结果。 Everything was fine without adding the fourth variable (bool 必需
)。我还尝试在发布 JSON 的 JavaScript 部分中将 true
更改为 "true"
,但我从服务器返回了相同的奇怪结果。使用 JavaScript 和 POSTMAN 时 POST 的结果是相同的,我认为问题出在服务器端代码。
任何帮助都会很棒。
这是我的 Controller 代码:
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
}
]
最佳答案
GetResult(List<Table> list)
<强> List<Table>
我认为这会让你失败,它是 System.Web.UI.WebControls 中的一个类,一个 webform 控件。将其更改为 List<SurveyDetails>
并尝试
我没有50分,无法添加评论,所以只留下答案
关于javascript - 意外结果,解析 ASP.Net 中的 JSON 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29002523/