How to send a list of data to Controller Action me

2019-06-09 13:30发布

Here is my Action Mehtod

public JsonResult GetById(IEnumerable<Guid> idList)
    {

        //.....
    }

And my JavaScript . I'm making an array of strings using li element's id property

var idArr = [];
        var list = $("#ulApplications li");
        $.each(list, function () {  idArr.push($(this).attr("id")) });          


        $.ajax(
            {
                type: "GET",
                url: "/rolemanagement/application/GetById/",
                contentType: false,
                datatype: "json",
                data: { 'idList': idArr },
                success:........

On my Action method I'm not getting any data.It seems I'm Missing something. thanks

3条回答
贪生不怕死
2楼-- · 2019-06-09 13:57

I tried out the below code which seems to be working fine,

$.ajax({
    type: "GET",
    url: "@Url.Action("GetById", new { area = "rolemanagement", controller = "application"})",
    data: { idList: [{ a: 'a' }, { a: 'b' }] },
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) { console.log(data); },
    error: function () { throw new Error("Error occurred."); }
});

which already given by @stephen where even stringify is not even required to send the data.

查看更多
男人必须洒脱
3楼-- · 2019-06-09 14:02

Change your ajax to

$.ajax({
  type: "GET",
  url: "/rolemanagement/application/GetById", // should use '@Url.Action(..)'
  dataType: 'json',
  contentType: "application/json; charset=utf-8",
  data: JSON.stringify({ idList: idArr }), // stringify
  success: ....
})
查看更多
我命由我不由天
4楼-- · 2019-06-09 14:17

After searching on stackoverflow and other blogs. I found a solution which is working for me, although above given answers are closely correct but they didn't worked for me. Here is my answer

var IdList = {idList:idArr}

        $.ajax(
            {
                type: "GET",
                url: "/rolemanagement/application/GetById/",
                contentType: false,
                datatype: "json",
                data: IdList,
                traditional: true,
                success:
                    function (data) {
                        alert(data);
                    },
查看更多
登录 后发表回答