2011-04-25 76 views
2

是否可以将JSON字符串数组发布到MVC3而不使用自定义绑定器?该阵列正在从复选框值创建和我通过jQuery如下张贴:将JSON数组绑定到ASP.NET MVC3

var checkedItems = $(":checked").map(function() { 
     return this.value; 
    }).get(); 

    $.post("/test/delete", { Items: checkedItems }); 

原始post查询字符串看起来是这样的:

Items%5B%5D=test1.com&Items%5B%5D=test2.com&Items%5B%5D=test3.com 

我看看MVC3源看起来我需要使用像[0],[1]这样的索引值。如果MVC3不能本地绑定数组,是否有一种简单的方法来更改后值,以便他们具有MVC3所需的索引值?

回答

3

你需要指定的内容类型asp.net的MVC所以这是它读成JSON,使用jQuery AJAX功能是这样的:

$.ajax({ 
     url: "/test/delete", 
     type: 'POST', 
     dataType: 'json', 
     data: JSON.stringify({Items: checkedItems}), 
     contentType: 'application/json; charset=utf-8', 
     success: function (data) { 

     } 
    }); 

,并得到json2.js使json.stringify可在浏览器不supoprt它

+0

谢谢!这正是我需要的。 – Mark 2011-04-25 20:48:16

0

您需要的阵列编码

{ Items: JSON.stringify(checkedItems) } 

然后它在另一边进行解码。但要小心--JSON.stringify在IE7中不可用。

+2

获取json2.js,它将添加JSON.stringify时,它不可用,不要触摸任何东西时,它会。 – ryudice 2011-04-25 20:31:32