通过Ajax发送数组并绑定到MVC 4上的List <string> </string>

时间:2014-09-11 14:11:24

标签: c# ajax asp.net-mvc asp.net-mvc-4 asp.net-ajax

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很好:它显示了&#34; aha!&#34;警报。我似乎无法为mvc活页夹提供准确的格式。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

更新您对此的ajax调用:

$.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选项。