通过Ajax将对象数组(Javascript)转换为C#
Array of Objects (Javascript) to C# via Ajax
我读过很多其他人在向C#后面的代码发送对象数组时遇到这个问题。
Javascript
--This constructs an array of objects called Shifts,
-- there is more too it but it is just how it iterates the divs for elements and such,
-- not important for this issue.
-- This is where the issue is, or I suspect in the way the data is sent and retrieved.
Shifts = JSON.stringify({ events: Shifts });
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: '/API/ManRoster',
data: Shifts,
error: function() {
$('#info').html('<p>An error has occurred</p>');
},
success: function(data) {
console.log(data);
},
type: 'POST'
});
console.log(Shifts);
转换原始数据
{"events":[{"ShiftID":"10","EVTDate":" 2016-04-15","UID":"1","Notes":"hgr"},{"ShiftID":"10","EVTDate":" 2016-04-15","UID":"1","Notes":"hgr"},{"ShiftID":"15","EVTDate":" 2016-04-15","UID":"1","Notes":"uyuy"},{"ShiftID":"15","EVTDate":" 2016-04-15","UID":"1","Notes":"uyuy"}]}
ManRoster.cs
[HttpPost("")]
public JsonResult Post(List<Models.Event> events)
{
try
{
_context.Events.AddRange(events);
return Json(true);
}
catch
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Failed");
}
}
Event.cs
public class Event
{
[Key]
public int EVTID { get; set; }
public int UID { get; set; }
public int ShiftID { get; set; }
public DateTime EVTDate { get; set; }
public string Notes { get; set; }
}
在ManRoster.cs中,我在事件中得到0行。所以在某个地方,数据在发送过程中丢失了。
在这个问题上任何帮助都将是巨大的。
编辑1
更改了Javascript
Shifts = JSON.stringify(Shifts);
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: '/API/ManRoster',
data: { events: Shifts },
error: function() {
$('#info').html('<p>An error has occurred</p>');
},
success: function(data) {
console.log(data);
},
type: 'POST'
});
console.log(Shifts);
假设您的预字符串化Shifts类似于:
[
{
"ShiftID": 10,
"EVTDate": "2016-04-15",
"UID": 1,
"Notes": "hgr"
},
{
"ShiftID": 10,
"EVTDate": "2016-04-15",
"UID": 1,
"Notes": "hgr"
},
{
"ShiftID": 15,
"EVTDate": "2016-04-15",
"UID": 1,
"Notes": "uyuy"
},
{
"ShiftID": 15,
"EVTDate": "2016-04-15",
"UID": 1,
"Notes": "uyuy"
}
]
您应该将[FromBody]属性添加到参数中,以告诉模型绑定器该值来自正文:
[HttpPost("")]
public JsonResult Post([FromBody]List<Models.Event> events)
{
try
{
_context.Events.AddRange(events);
return Json(true);
}
catch
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Failed");
}
}
您的javascript应该看起来像:
Shifts = JSON.stringify(Shifts);
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: '/API/ManRoster',
data: Shifts,
error: function () {
$('#info').html('<p>An error has occurred</p>');
},
success: function (data) {
console.log(data);
},
type: 'POST'
});
console.log(Shifts);
相关文章:
- 使用键/代码转换JavaScript数组
- NS_ERROR_XPC_BAD_CONVERT_JS:无法转换JavaScript参数arg 0[nsIDOMWind
- 转换Javascript中的DayOfYear值以用于Flot
- 转换Javascript日期格式
- 字符串大小写转换 javascript
- 一个很好的文本到语音转换JavaScript库
- JQuery 函数突然停止工作,转换 Javascript 参数时出错
- 转换 JavaScript 代码
- 需要转换Javascript数组的格式
- 转换javascript中的日期格式
- 循环以转换JavaScript对象
- jQuery Post中的错误:无法转换JavaScript参数
- UglifyJS转换javascript代码
- Html到Haml的转换javascript
- 正在转换Javascript日期
- 转换Javascript关联对象并用PHP输出
- 转换JavaScript'这'到jQuery'$(这个)'
- 转换JavaScript数组
- 无法转换JavaScript参数arg 0 [nsIDOMWindow.getComputedStyle]
- jQuery美元.Firefox中的ajax错误:"无法转换JavaScript参数"