Webapi parameter is null for the method

I am trying to send back a list of data objects (WorkOrders) in JSON format to my Webapi controller, this works surprisingly well, with the slight drawback that the parameter of the data object (savemodel) is null when it gets to the webapi controller. This is an excerpt from JS (slots - data layout)

var slots = [];    
            slots.push({ 'WorkOrder': 'XX21', 'OrderDate': '2015-10-11 00:00:00', 'Slot': '1', 'SageRef': 'HS11' });
            slots.push({ 'WorkOrder': 'XX22', 'OrderDate': '2015-10-12 00:00:00', 'Slot': '2', 'SageRef': 'HS12' })
            slots.push({ 'WorkOrder': 'XX23', 'OrderDate': '2015-10-13 00:00:00', 'Slot': '3', 'SageRef': 'HS13' });
            console.log(JSON.stringify({ 'savemodel': slots }))
            $.ajax({
                type: "POST",
                url: 'http://localhost:55821/api/schedule',
                data: JSON.stringify({ 'savemodel': slots }),
                contentType: 'application/json; charset=utf-8'                
            }).success(function (data) {
                $scope.$apply(function () {
                    if (data.SaveMessage.length > 0) {
                      // do something
                    }
                    else {
                      // do something else
                    }
                });
            });

Model:

    public class WorkOrderModel
{
    public string WorkOrder { get; set; }
    public string OrderDate { get; set; }
    public string SlotNumber { get; set; }
    public string SageRef { get; set; }
}

Postback Method:

 [HttpPost]
        public IHttpActionResult UpdateWorkOrder([FromBody]List<WorkOrderModel> savemodel)
        {
            StringBuilder saveMessage = new StringBuilder();
            foreach (WorkOrderModel model in savemodel)
            {
                   // save the WO model object  here                
            }
            if (saveMessage.Length > 0)
            {
                return Ok(new { SaveMessage = "There were issues: " + saveMessage.ToString() });
            }
            else
            {
                return Ok(new { SaveMessage = "" });
            }
        }

Added JSON Null parameter in WebApi controller This is driving me crazy, so any help would be greatly appreciated at this point!

Regards, itsdanny

+4
source share
1 answer

Sorted, it changed

: JSON.stringify({'savemodel': slots}),

to

: JSON.stringify(),

+1

Source: https://habr.com/ru/post/1613370/


All Articles