开发者

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


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


You may take a look at the following blog post. Hopefully it will clarify things up. Basically there are two cases: ASP.NET MVC 3 where you can send JSON requests to controller action out of the box and previous ASP.NET MVC versions where you need to use a custom JsonValueProviderFactory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜