4

I'm running C# with MVC in ASP.NET, and I've got an ajax call that's supposed to return a list of objects, but instead it's just returning the string "System.Collections.Generic.List`1[Namespace.CustomObject]"

Clearly the data is making it back to the javascript, and returning List<int> doesn't change anything significant, so the object isn't at fault. Did I make a mistake in my ajax call that I've been missing, or do I need to use something other than list?

My ajax call:

$.ajax({
        type: 'POST',
        url: url,
        data: arrayOfInts
        contentType: 'application/json',
        datatype: 'json',
        success: function (data) {
            if (data.length) {
                doThisIfDataReturned(data);
            } else {
                doThisIfNoDataReturned(productIds);
            }
        }
    });

And the method it calls:

public List<CustomObject> MakeAList(int[] productIds)
    {
        //
        // create objectList
        //
        return objectList; //debugger shows that list is correct here
    }
7
  • 2
    You need something in place that will perform the serialization of the object. What are you running server side? MVC? WebForms? How specifically does your request end up at MakeAList? Commented Jul 24, 2015 at 16:16
  • if you ask for json from client...need to respond with json Commented Jul 24, 2015 at 16:19
  • Something looks off with the serialization. It's doing the .ToString() representation of the list instead of the JSON serialized version of the return value. Can you show abbreviated code for your service including any attributes applied to the class/method? Commented Jul 24, 2015 at 16:21
  • possible duplicate of Turn C# object into a JSON string in .NET 4 Commented Jul 24, 2015 at 16:23
  • @Rod Probably not - there can be some very specific ways of encoding objects as json depending on what's going on here - may not need to resort to using manual serialization. There's not enough information here to know if that's the case or not though. Commented Jul 24, 2015 at 16:25

1 Answer 1

1

In the C# you need to return a JSON object instead of a List. I've done stuff like this in the past:

    public JsonResult myFunc()
    {
        ..... code here .....


        return Json(myList);
    }

Edit: Sometimes it is nice to see exactly what is being sent back before it is sent. One way to accomplish this is to assign the return object to a variable, then return the variable.

    public JsonResult myFunc()
    {
        ..... code here .....

        var x = Json(myList);
        return x;
    }

This does exactly the same thing but is slightly easier to debug.

Sign up to request clarification or add additional context in comments.

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.