开发者

How do i pass array object in action method?

I'm getting error in the method $.toJSON(batch) that object does not support this property or method. How can i pass batch array using $.toJASON(batch) method?

$.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: '@Url.Action("BatchUpdate", "Home")',
            dataType: 'json',
            //data: '{viewModelsBatch: '+batch+'}',
            data: $.toJSON(batch)开发者_如何转开发,
            success: function(result) {
              //...
              $('#jqgProducts').trigger('reloadGrid');
            }
    });


I agree with Paul Grime that the usage of JSON.stringify from json2.js is better as toJSON jQuery plugin. The reason is that JSON.stringify is native implemented in the most web browsers. You should include json2.js only to be sure that if the web browser do have no support of JSON.stringify the implementation in JavaScript will be used. The typical jQuery plugin is on the other side pure JavaScript code. So the serialization will be slowly. Moreover the author of json2.js is Douglas Crockford - the author of JSON standard and very famous specialist in JavaScript language. So his implementation of JSON serialization in JavaScript is really the best.

Your main question was another: why data: $.toJSON(batch) or data: JSON.stringify(batch) will not work in your code. The problem is not jqGrid problem, but pure server side problem. You don't wrote what technology you use on the server side. The code looks like ASP.NET MVC code, but it's unclear which version of ASP.NET MVC you use. If you use version 3.0 it should work automatically, but in case of 2.0 version you need register custom JsonValueProviderFactory (see here).


I don't know what the toJSON function is, so I'd recommend reading this answer.

Serializing to JSON in jQuery

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜