user979331
user979331

Reputation: 11851

jQuery post wont post data to ASP.NET API Controller

I am having a nightmare of a time sending data to ASP.NET Controller via jquery post. This is what the data looks like after JSON.stringify:

[{"scheduleTaskID":"203","task":"Permit","baselineDate":"4/6/2005 8:00:00 AM","scheduledDate":"4/6/2005 8:00:00 AM","actualDate":"4/6/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"195","task":"Office Files","baselineDate":"7/13/2005 8:00:00 AM","scheduledDate":"7/13/2005 8:00:00 AM","actualDate":"7/13/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"196","task":"Foundation","baselineDate":"7/27/2005 8:00:00 AM","scheduledDate":"7/27/2005 8:00:00 AM","actualDate":"8/13/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"197","task":"Framing","baselineDate":"8/5/2005 8:00:00 AM","scheduledDate":"8/5/2005 8:00:00 AM","actualDate":"8/23/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"198","task":"Finishes Exterior","baselineDate":"8/26/2005 8:00:00 AM","scheduledDate":"8/26/2005 8:00:00 AM","actualDate":"9/14/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"199","task":"Drywall","baselineDate":"9/2/2005 8:00:00 AM","scheduledDate":"9/2/2005 8:00:00 AM","actualDate":"9/16/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"200","task":"Flooring","baselineDate":"9/1/2005 8:00:00 AM","scheduledDate":"9/1/2005 8:00:00 AM","actualDate":"9/20/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"201","task":"General Finish","baselineDate":"9/12/2005 8:00:00 AM","scheduledDate":"9/12/2005 8:00:00 AM","actualDate":"","finishedDate":"","selected":"on"},{"scheduleTaskID":"202","task":"Final PDI","baselineDate":"10/11/2005 8:00:00 AM","scheduledDate":"10/11/2005 8:00:00 AM","actualDate":"","finishedDate":"","selected":"on"},{"scheduleTaskID":"203","task":"Permit","baselineDate":"4/6/2005 8:00:00 AM","scheduledDate":"4/6/2005 8:00:00 AM","actualDate":"4/6/2005 8:00:00 AM","finishedDate":"","selected":"on"},{}]

This is how I am trying to pass that data:

$.post("/api/update/", JSON.stringify( array ), alert('success'), 'json');

This is my ASP.NET API Controller:

[HttpPost]
        public dynamic Post(List<CellModel> cells)
        {
        }

This is what CellModel is:

public class CellModel
    {
        public string scheduleTaskID { get; set; }
        public string task { get; set; }
        public string baselineDate { get; set; }
        public string scheduledDate { get; set; }
        public string actualDate { get; set; }
        public string finishedDate { get; set; }
        public bool selected { get; set; }
    }

When I put a break point in the controller after public dynamic Post(List<CellModel> cells) it says cells is cells Count = 0...I put return false; after my post call to see a network call, it says the Status Code is 301 Moved Permanently:

enter image description here

This is how I am getting this data:

$("#form").submit(function (event) {
            var array = [];
            $('#form > table > tbody  > tr').each(function (elem) {
                var item = {
                    scheduleTaskID: $(this).find("td > #scheduleTaskID").val(),
                    task: $(this).find("td > #task").val(),
                    baselineDate: $(this).find("td > #baselineDate").val(),
                    scheduledDate: $(this).find("td > #scheduledDate").val(),
                    actualDate: $(this).find("td > #actualDate").val(),
                    finishedDate: $(this).find("td > #finishedDate").val(),
                    selected: $(this).find("td > #selected").val(),
                };
                array.push(item);
            });

            $.post("/api/update/", JSON.stringify(array), alert('success'), 'json');
            return false;

        });

Upvotes: 0

Views: 78

Answers (1)

Tian van Heerden
Tian van Heerden

Reputation: 639

POSTing a straight-up JSON array to an MVC controller doesn't work. The solution is to use the long-form AJAX jQuery method, include the traditional:true option, and structure your JSON data and model differently - see this answer.

Re the model: the parameter to your controller action should not be an array - it should be a model class which contains and array member - and of course your JSON should mirror this structure.

Upvotes: 1

Related Questions