Ajax call returning a string describing an object instead of an object

I am running C # with MVC in ASP.NET and I have an ajax call that should return objects list

, but instead it just returns a string"System.Collections.Generic.List`1[Namespace.CustomObject]"

Obviously the data is returning it in javascript, and the return List<int>

doesn't change anything significant, so the object is not to blame. I made a mistake in my ajax call that I am 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
    }

      

+3


source to share


1 answer


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

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


        return Json(myList);
    }

      

Edit: Sometimes it's nice to see exactly what is being sent back before it is sent. One way to achieve this is to assign the returned object to a variable and then return the variable.



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

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

      

This does the same thing, but is a little easier to debug.

0


source







All Articles