I'm not sure why this is happening. I have a string array that should be sent to a controller action that's expecting a string array. This is my jQuery:
我不知道为什么会这样。我有一个字符串数组应该被发送到一个控制器动作,它期望一个字符串数组。这是我的jQuery:
$.post("/@Model.Controller/@Model.Action", { "choices": ajax }, function (result) {
$("#label" + "@Model.Id").html(result);
});
This is my controller action:
这是我的控制器动作:
public JsonResult UpdateMultipleType(string[] choices)
{
return Json(choices);
}
I took a look in Firebug and in the Post Tab, the headers read:
我查看了Firebug,在Post选项卡中,标题是:
Parametersapplication/x-www-form-urlencoded
choices[] Comedy
choices[] Thriller
choices[] Action
choices[] Adventure
I've debugged and confirmed that it is hitting UpdateMultipleType and that the string array "choices" is null when that action is called. The call goes through but since we're returning null, I get a Javascript error after the call completes.
我已经调试并确认它正在命中UpdateMultipleType,并且当调用该操作时,字符串数组“choices”为空。调用会执行,但是由于我们返回null,所以在调用完成后会出现Javascript错误。
I don't know why my controller action is being sent null when it's clear that there is an array called choices being sent over.
我不知道为什么我的控制器动作被发送为null,当有一个名为options的数组被发送过来时。
3 个解决方案
#1
12
you need to tell jQuery to use the traditional way of building ajax data.
您需要告诉jQuery使用构建ajax数据的传统方式。
put this in your document ready:
在你的文件中准备好:
jQuery.ajaxSettings.traditional = true;
#2
3
Javascript:
Javascript:
var data = ["a", "b", "c", "d"]; //sample
//Wrap your data ...
var postData = { choices: data };
$.ajax({
type: "POST",
url: "/Test/Index", //Your URL here
data: postData,
success: function (result) {
//your code here
},
dataType: "json",
traditional: true
});
Your controller:
你的控制器:
public JsonResult UpdateMultipleType(List<String> choices) [...]
看到它。
#3
-2
You need to decorate the Action with attribute with [HttpGet] and pass the second param for Json as Allowget
您需要使用[HttpGet]来修饰操作,并将Json的第二个参数作为Allowget传递
[HttpGet]
public JsonResult UpdateMultipleType(string[] choices)
{
return Json(choices, JsonRequestBehavior.AllowGet);
}
If choices are array can you change the 'Choices' to 'Choices[]'
如果选项是数组,你能把“选项”改成“选项[]”吗?
#1
12
you need to tell jQuery to use the traditional way of building ajax data.
您需要告诉jQuery使用构建ajax数据的传统方式。
put this in your document ready:
在你的文件中准备好:
jQuery.ajaxSettings.traditional = true;
#2
3
Javascript:
Javascript:
var data = ["a", "b", "c", "d"]; //sample
//Wrap your data ...
var postData = { choices: data };
$.ajax({
type: "POST",
url: "/Test/Index", //Your URL here
data: postData,
success: function (result) {
//your code here
},
dataType: "json",
traditional: true
});
Your controller:
你的控制器:
public JsonResult UpdateMultipleType(List<String> choices) [...]
看到它。
#3
-2
You need to decorate the Action with attribute with [HttpGet] and pass the second param for Json as Allowget
您需要使用[HttpGet]来修饰操作,并将Json的第二个参数作为Allowget传递
[HttpGet]
public JsonResult UpdateMultipleType(string[] choices)
{
return Json(choices, JsonRequestBehavior.AllowGet);
}
If choices are array can you change the 'Choices' to 'Choices[]'
如果选项是数组,你能把“选项”改成“选项[]”吗?