javascript - 使用 jQuery Post 将 JavaScript 数组发布到采用 IEnumerable<T> 的 ASP.NET MVC 操作

标签 javascript asp.net-mvc jquery

假设我有以下 javascript 来执行帖子:

$.post("/MyController/SomeAction", 
      { myParam: ['Filip', 'Ekberg'] }, function(data) { alert(data); }, "html");

我的操作如下所示:

[HttpPost]
public ActionResult SomeAction(FormCollection collection, 
                               IEnumerable<string> myParam)
{
    return null;
}

当我输入此操作时,myParam 为空,如果我展开 FormCollection,我会看到以下内容:

enter image description here

这里奇怪的部分是名称(Key)是 myParam[]这可能就是它没有映射到 myParam 的原因.

另外,我尝试做 dynamic[] myParam也可以,但也不起作用。

我知道我可以使用JSON.stringify但我现在不想这样做。那么,您知道这里发生了什么以及是否有解决方案?

最佳答案

尝试将traditional参数设置为true:

$.ajax({
    url: '/MyController/SomeAction',
    type: 'POST',
    data: { myParam: [ 'Filip', 'Ekberg' ] },
    traditional: true,
    success: function(data) {
        alert(data);
    }
});

您还可以安全地从 POST 操作签名中删除 FormCollection 参数。没啥用。

关于javascript - 使用 jQuery Post 将 JavaScript 数组发布到采用 IEnumerable<T> 的 ASP.NET MVC 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7951267/

相关文章:

javascript - jasmine running 和 waitsFor 实际上做了什么?

Javascript 事件 addEventListener 最后一个参数

javascript - 按 div 内的类名选择所有 div

asp.net-mvc - 用 ASP.NET MVC 实现 MEF?

c# - 在 C# 中使用带有数字节点的 JSON.decode razor webhelper?

javascript - jQuery CheckBoxes Price 增删改查

javascript - 使用 Web API 在 CRM 中添加新记录

javascript - 视频背景不起作用(html、css、js)

JQuery 淡出/删除

c# - 在 javascript 中获取 Usercontrol 内控件的客户端 ID