Mark C.
Mark C.

Reputation: 6460

Json method returns Object literal, expected JSON

I'm trying to return JSON, to map to a model, however I can't figure out why my method is returning an object literal and not JSON.

I threw together a Fiddle that shows the current format I am receiving data.

Controller method

 public JsonResult GetDeferredAccountDetailsByAccount(int id)
        {
            var details = _deferredAccountDetailsService.GetDeferredAccountDetailsByAccount(id);

            return Json(details, JsonRequestBehavior.AllowGet);
        }

This returns :

enter image description here

..And in the browser :

enter image description here

In the Fiddle I linked, simply wrapping the object literal in [] allows Knockout to interpret the object just fine, but without it fails.

Is there something I am doing incorrectly or a reason why I'm not receiving JSON? Do I need to return an ICollection or something for it to be interpreted as JSON?

I looked around but couldn't really find anything.

Upvotes: 1

Views: 80

Answers (1)

Fals
Fals

Reputation: 6839

You expects an Array, but you are returning a literal object at the controller. And you are biding a collection using knockout, but accounts It's a literal. That's why everything works when you put [] at the JSON.

You should just push every property from the JSON to an Array instead of _map, or fix the _map function to bind property to an Array!

Upvotes: 1

Related Questions