4

I'm trying to post back a list of data objects (WorkOrders), in JSON format to my Webapi controller, this works amazingly well, except for the slight flaw in that the data object parameter (savemodel) is null when it hits the webapi controller. This is a snip from the JS (slots is mock data)

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
                    }
                });
            });

The model:

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

The 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 = "" });
            }
        }

Posted JSON Null parameter in the WebApi Controller This is driving me nuts so any help will be hugely appreciated at this stage!

Regards, itsdanny

4
  • could you post a snippet code of your Post controller method Commented Oct 27, 2015 at 16:35
  • Hi, I've updated the original post. Commented Oct 27, 2015 at 19:01
  • I think you need to set dataType: 'json' in your ajax call. See AJAX & Web Api Post Method - How does it work?. Commented Oct 28, 2015 at 5:43
  • Thanks @dbc but I still get a null parameter when it hits the post method. I've also tried [FromUrI] but it’s still null. Commented Oct 28, 2015 at 8:17

1 Answer 1

1

Sorted, it changed

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

to

data: JSON.stringify(slots),

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.