将数组从jQuery传递给MVC.NET控制器,在控制器上给出null结果,但jQuery函数上存在值

时间:2022-12-05 19:58:19

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.

我正在尝试将数组从jQuery函数传递给我的控制器。该数组包含内容和持有该内容的div的id。

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.

当我检查通过Firebug中的AJAX帖子发送的对象时,正确的值存在但是在我的控制器上放置一个断点后,接收的值是一个空的List或Array或我尝试将其设置为的任何类型。我很擅长使用JSON将数据传递给我的控制器,所以在我出错的地方会有所帮助。

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.

jQuery函数调用“提交”单击。该数组在我的脚本中全局声明,并在每次用新内容填充新区域时添加。

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

2 个解决方案

#1


1  

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.

您可以查看以下博客文章。希望它能澄清一切。基本上有两种情况:ASP.NET MVC 3,您可以将JSON请求发送到开箱即用的控制器操作以及之前需要使用自定义JsonValueProviderFactory的ASP.NET MVC版本。

#2


5  

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

我通过在进行调用之前将传统属性设置为true来实现此功能。即:

jQuery.ajaxSettings.traditional = true

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

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

我在这里找到了解决方案:通过AJAX将数组传递给mvc Action

#1


1  

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.

您可以查看以下博客文章。希望它能澄清一切。基本上有两种情况:ASP.NET MVC 3,您可以将JSON请求发送到开箱即用的控制器操作以及之前需要使用自定义JsonValueProviderFactory的ASP.NET MVC版本。

#2


5  

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

我通过在进行调用之前将传统属性设置为true来实现此功能。即:

jQuery.ajaxSettings.traditional = true

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

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

我在这里找到了解决方案:通过AJAX将数组传递给mvc Action