通过Ajax将对象数组(Javascript)转换为C#

Array of Objects (Javascript) to C# via Ajax

本文关键字:转换 Javascript Ajax 对象 数组 通过      更新时间:2024-03-13

我读过很多其他人在向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);