我正在尝试接收多个选择框中选定的值。通过Ajax电话。
下面是我的测试动作
public ActionResult MultiSelect(String[] test)
{
String[] arrayornot = test; //null being recieved. or the string if hardcoded
}Jquery
alert($('#county').val()); // London, Brim
$.ajax({
url: '@Url.Action("MultiSelect", "APITest")',
type: 'GET',
cache: false,
data: { test: $('#county').val()},
success: function (result) {
$('#myDiv').html(result);
}
});如果我把它硬编码成一个字符串,它就能正常工作。使用String[]或String端点。如果它以逗号分隔的字符串传递,我可以在服务器端对其进行排序。或者字符串数组更好。
发布于 2014-02-06 20:08:51
我和你有同样的问题。我在下面的链接中找到了答案。
http://dovetailsoftware.com/clarify/kmiller/2010/02/24/jquery-1-4-breaks-asp-net-mvc-actions-with-array-parameters
基本上,您需要做的是添加以下一行,这些值将作为数组传递。
jQuery.ajaxSettings.traditional =真;
发布于 2013-12-05 15:14:50
错误是$Ajax配置集传统的方法: true,那么您的问题就解决了。
var selectedItems=$('#county').val();
$.ajax({
url: '@Url.Action("MultiSelect", "APITest")',
type: 'POST',
cache: false,
traditional: true,
data: { test: JSON.stringify(selectedItems)},
success: function (result) {
$('#myDiv').html(result);
}
});发布于 2013-09-24 09:28:46
我会使用javascript数组并转换成JSON字符串
var selectedItems=$('#county').val(); // returns the array of selected items然后使用JSON.stringify方法
$.ajax({
url: '@Url.Action("MultiSelect", "APITest")',
type: 'GET',
cache: false,
data: { test: JSON.stringify(selectedItems)},
success: function (result) {
$('#myDiv').html(result);
}
});JSON.Stringify在IE7中不可用。请使用JSON2.js
希望这能帮到你!
https://stackoverflow.com/questions/18977243
复制相似问题