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

时间:2016-04-08 04:43:33

标签: javascript c# jquery 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);

1 个答案:

答案 0 :(得分:1)

假设您的预先连接的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);