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
    }
octothorpentine
  • 319
  • 2
  • 18
  • 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`? – James Thorpe Jul 24 '15 at 16:16
  • if you ask for json from client...need to respond with json – charlietfl Jul 24 '15 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? – Colin Jul 24 '15 at 16:21
  • possible duplicate of [Turn C# object into a JSON string in .NET 4](http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4) – Rod Jul 24 '15 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. – James Thorpe Jul 24 '15 at 16:25
  • @JamesThorpe MVC. Will edit that into the question. – octothorpentine Jul 24 '15 at 16:30
  • `public JsonResult MakeAList(int[] productIds) { .... return Json(objectList);` }` –  Jul 25 '15 at 12:00

1 Answers1

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.

nurdyguy
  • 2,876
  • 3
  • 25
  • 32