如何将List值从Ajax调用传递给控制器

时间:2014-09-26 05:05:43

标签: jquery ajax list

我想将List值从Ajax调用传递给控制器​​。值以逗号分隔。但是当它来到控制器时,我希望它会像列表一样显示。但它以逗号显示。

我的代码

function Activate() {
    var orderId = "";
    var count = 0;
    $("input:checkbox[name=ActivateOrder]:checked").each(function () {
        if (count == 0) {
            orderId = $(this).val();
        }
        else {
            orderId += "," + $(this).val();
        }
        count = count + 1;
    });

    var data = "orderIds=" + orderId;
    var url = '<%= Url.Action("activate", "Report","Add") %>';

    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        success: function (data) { 
            if(data.error=="3")
            {
                alert(data.message);
                document.getElementById('checkall').checked=false;
                 window.location = "/Add/Report/savedetails?lstOrderIds=" + orderId;
            }

        }
    });
    return false;

}

我必须将lstOrderIds传递给控制器​​

 public ActionResult savedetails(List<int> lstOrderIds)
{
            //Implement some logic..
}

如何转换?我尝试了很多方法。但没有希望。我想我解决这个问题并不是那么天才。帮我看看..

1 个答案:

答案 0 :(得分:3)

创建如下的数组。

    var selected = new Array();
    $('input:checked').each(function () {
        selected.push($(this).attr('id'));
        alert("Event " + $(this).attr('id') + " will be compared");
    });

    $.ajax({
        url: "/Event/CompareEvents",
        contentType: "application/json",
        type: "POST",
        datatype: "json",
        data: JSON.stringify({eventIds:selected}),
        error: function (xmlHttpRequest, errorText, thrownError) {
            alert(xmlHttpRequest, errorText, thrownError);
        },
        success: function (data) {
            alert("success");
            document.location = data;
        }
    });

控制器代码

   [HttpPost]
   public ActionResult CompareEvents(List<int> eventIds)
{
    return null;
}