Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

c# - Send array to MVC controller via JSON?

I am trying, and struggling, to send an array via JSON to a MVC controller action.

Here is what I have and what i've tried...

//Get checked records
var $checkedRecords = $(':checked'); //e.g. 3 rows selected = [input 4, input 5, input 6]

//Have tried following:
sendingVar: $checkedRecords.serializeArray(); // gives array of 0's
sendingVar: JSON.stringify($checkedRecords); // gives "{"length":1,"prevObject":{"0":{"jQuery1313717591466":1,"jQuery1313717591653":13},"context":{"jQuery1313717591466":1,"jQuery1313717591653":13},"length":1},"context":{"jQuery1313717591466":1,"jQuery1313717591653":13},"selector":":checked","0":{}}"...wtf

//Post
$.post(url, { sendingVar: sendingVar }, function(data) {alert(data); });

How do I do it ?

edit: to those that suggest sending $checkedRecords "as is" from the top line - that is not working. I get a strange exception somewhere in the jquery framework :(

uncaught exception: [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://.../.../.../jquery-1.4.4.min.js :: <TOP_LEVEL> :: line 141" data: no]

which I think means it is attempting to assign null to something it cannot.

Edit: i'm using MVC2 not 3

Edit2: After @Monday's answer- the problem is due to how I have built the array like [input 4, input 5, input 6] and not [4,5,6] - any ideas how I can just get the values in the array instead ?

Edit3: Stop voting up duplicate when it's not. Did you actually read my problem or read the problems linked? this is a different issue

@Daveo:

I don't want to build in an overriding custom attribute just to send an array from JSON, that is rediculous as we've already covered in this question-it is not necessary.

MVC3 - irrelevant

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is my demo,use mvc2,hope some helps~

The key to success is traditional

set the traditional parameter to true

$(function(){
    var a = [1, 2];
    $.ajax({
       type: "POST",
       url: "<%= ResolveUrl("~/Home/PostArray/") %>",
       data: {orderedIds: a},
       dataType: "json",
       traditional: true,
       success: function(msg){alert(msg)}
    });
})

Since jquery 1.4 this parameter exists because the mechanism to serialize objects into query parameters has changed.

and action is~

[HttpPost]
public ActionResult PostArray(int[] orderedIds)
{
    return Content(orderedIds.Length.ToString());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...