Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Array from jQuery to MVC.NET Controller giving null result on controller but values present on jQuery function

I'm trying to pass an array from a jQuery function to my controller. The array contains content and the id of the div holding that content.

When I check the objects which are being sent via the AJAX post in Firebug the correct values are there but after placing a breakpoint on my controller the received value is an empty List or Array or whatever type I try to set it to. I'm fairly new to using JSON to pass data to my controllers so would appreciate some help in where I'm going wrong.

jQuery function called on "submit" click. The array is globally declared in my script and is added to each time a new area is filled with content.

function postContent() {
        $.ajax({
            type: "POST",
            datatype: 'json',
            url: "/Admin/getContentArray",
            data: JSON.stringify(contentArray),
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                alert(result.Result);
            }
        });
    }

Test receiving controller

[HttpPost]
    public JsonResult getContentArray(List<Content> myPassedArray)
    {
        var data = myPassedArray;
        return this.Json(null);
    }
like image 644
Mark OB Avatar asked Apr 02 '11 18:04

Mark OB


1 Answers

I got this working by set the traditional property to true before making the get call. i.e.:

jQuery.ajaxSettings.traditional = true

$.get('/controller/MyAction', { vals: arrayOfValues }, function (data) {... 

I found the solution here: Pass array to mvc Action via AJAX

like image 144
John Avatar answered Oct 16 '22 06:10

John