通过Ajax发送数组并绑定到List< string>在MVC 4上 [英] Send array via Ajax and bind to List<string> on MVC 4
本文介绍了通过Ajax发送数组并绑定到List< string>在MVC 4上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
AJAX:
function systemsChanged(elem)
{
var items = [];
for (i = 0; i < elem.length; i++)
if (elem[i].selected) {
items.push(elem[i].value);
}
var post = { ids: items };
$.ajax({
type: "POST",
traditional: true,
url: "@Url.Action("LoadApplicationConfigs", "Application")",
content: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(post),
success: function (d) {
alert(d.message);
},
error: function (xhr, textStatus, errorThrown) {
alert("não rolo");
}
});
}
C#MVC 4:
[HttpPost]
public JsonResult LoadApplicationConfigs(IList<string> ids)
{
object data = new
{
message = "aha!"
};
return new JsonResult() { Data = data };
}
它输入post方法,但ids
设置为null
.我还尝试将ids
更改为string[]
,但未成功.返回ajax很好:它显示啊哈!"警报.我只是似乎无法为MVC资料夹提供准确的格式.有什么想法吗?
It enters the post method but ids
is set to null
. I also tried changing ids
to string[]
, not successfully. The return to ajax is fine: it shows the "aha!" alert. I just cant seem to be providing the accurate format to the mvc binder. Any thoughts?
推荐答案
将您的ajax调用更新为:
Update your ajax call to this:
$.ajax({
type: "POST",
traditional: true,
url: "@Url.Action("LoadApplicationConfigs", "Application")",
content: "application/json; charset=utf-8",
dataType: "json",
data: post,
success: function(d) {
alert(d.message);
},
error: function(xhr, textStatus, errorThrown) {
alert("não rolo");
}
});
无论是否使用traditional
选项,它都可以使用.
This will work with or without the traditional
option.
这篇关于通过Ajax发送数组并绑定到List< string>在MVC 4上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文