我想,並努力,通過JSON發送一個數組到MVC控制器的行動。通過JSON發送數組到MVC控制器?
這裏是我有什麼,什麼我已經試過......
//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框架:(
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]
,我認爲意味着它正試圖分配空的東西它不能
編輯:我使用MVC2不是3
EDIT2:@後周一的答案 - 的問題是由於我怎麼都內置了陣列狀[input 4, input 5, input 6]
而不是[4,5,6]
- 任何想法我怎麼能剛剛得到的陣列,而不是在值
編輯3:停止投票重複,當它不是。您是否真的閱讀過我的問題或閱讀過相關的問題?這是一個不同的問題
@Daveo:
I don't want to build in an overriding custom attribute just to send an array from JSON, that is rediculous正如我們已經覆蓋了這個問題,這是沒有必要的。
可能重複的:http://stackoverflow.com/questions/320291/how-to-post-an-array-複雜的對象與json-jQuery的 - 到asp網絡mvc控制和http://stackoverflow.com/questions/4789481/post-an-array-of-objects-via-json-to- asp-net-mvc3 – Daveo
你想要的結果是什麼?你的數組是jQuery對象,stringify版本對我來說很合適。你期待它看起來像什麼? – BZink
它只是一個包含檢查項目的字符串ID的數組。我想checkedRecords包含所有選中項目ID的數組。所選輸入的「值」包含我需要的ID值。 – baron