0

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;

        });
6
  • The URL to which you ware posting is update but the endpoint is Post, according to the code posted. Also, it appears as though you have multiple elements with the same id. This is a bad thing in HTML. Commented Oct 23, 2014 at 14:21
  • Yes multiple elements have a same id and I need to pass it in that way Commented Oct 23, 2014 at 14:23
  • I also don't understand how the Post method gets called. Looks like the data should be posted to the 'update' method... Commented Oct 23, 2014 at 14:32
  • its an api controller so the url should look like /api/<controllername>/<methodname> I get the same results if the put in the method name or take it out. the contoller is called UpdateController...only update should be in the url Commented Oct 23, 2014 at 14:38
  • >net web api uses magic words for crud . if the method starts with the verb then it is considered the verb public Task<Object> Post(Object object) is a Post Commented Oct 23, 2014 at 14:47

1 Answer 1

1

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.

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.