0

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

1
  • It is trying to map the URL's querystring to a list of GUIDs, which is messy to say the least :) Use post with your array instead to simplify this. Just use data: idArr and it should map the only data item Commented Dec 11, 2014 at 12:13

3 Answers 3

6

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: ....
})
Sign up to request clarification or add additional context in comments.

3 Comments

Still not working,I think default model binder is not able to convert the received data into a list of Guid.
Are the values valid GUID's? Try changing it to IEnumerable<string> idList (must admit I have never tried to pass GUID's to a controller)
Also for testing, create a dummy list in the client, say var idArr = ["Item 1", "Item 2"];
0

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.

Comments

0

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);
                    },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.