如何从ajax调用中调用List <string>类型参数方法?

时间:2016-05-19 21:19:53

标签: javascript c# jquery ajax

我想从jquery谈论C#方法(下面)的ajax调用?下面的代码(js)无法向“SessionTemplate(列表列表)”发送值。我怎样才能传递价值清单?

[HttpPost]
public bool SessionTemplate(List<string> list)
{
    HttpContext.Current.Session["templates"] = list;
    return true;
}

调用方法:(不工作)

function fncsave() {

    var arrtemplate = [];
    $('#mytemplateTags li').each(function () {
        var str = $(this).html();
        var res = str.match("<span class=\"tagit-label\">(.*?)</span>");
        if (res!=null) {
            var str = res[1];
            alert(str);
            arrtemplate.push(str);
        }
    });

    console.log(arrtemplate);
    var jsondata = { arrtemplate };

    $.ajax({
        url: "/api/TagCloud/SessionTemplate",
        method: "Post",
        data: jsondata,
        async: false,
        dataType: "json",
        success: function (msg) {
            console.log(msg);
            if (msg == true) {

                alert("true");
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

您的JSON数据没有包含列表名称的属性。因此,WebAPI无法将其映射到action参数。添加该属性。

var jsondata = { list: arrtemplate };
相关问题