问题描述
我想,和挣扎,通过JSON发送数组到MVC控制器动作。
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); });
我该怎么办呢?
编辑:那些建议发送 $ checkedRecords
原样从上线 - 这是行不通的。我什么地方得到一个奇怪的例外jQuery框架:(
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]
我认为意味着它正试图将null分配的东西它不能。
which I think means it is attempting to assign null to something it cannot.
编辑:我使用MVC2不是3
i'm using MVC2 not 3
EDIT2:@周一的答案 - 后的的问题是由于我怎么都建立像 [输入4,输入5,输入6] 而不是
[4,5,6]
- ?任何想法我怎么能刚刚得到的数组中的值,而不是
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不想一个压倒一切的自定义属性建立只是从发送JSON数组,这是因为我们已经包括在这个荒谬的问题,这是没有必要的。
MVC3 - 无关
推荐答案
下面是我的演示,使用MVC2,希望有帮助〜
Here is my demo,use mvc2,hope some helps~
要成功的关键是
设置
传统的
参数设置为true
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)}
});
})
由于jQuery的1.4这个参数存在,因为序列化对象到查询参数的机制已经改变了。
Since jquery 1.4 this parameter exists because the mechanism to serialize objects into query parameters has changed.
和行动〜
[HttpPost]
public ActionResult PostArray(int[] orderedIds)
{
return Content(orderedIds.Length.ToString());
}
这篇关于通过JSON发送到阵列控制器的MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!