How to get JsonResult data

I have the following action in my controller layouts

public JsonResult getlayouts(int lid)
{
    List<layouts> L = new List<layouts>();
    L = db.LAYOUTS.Where(d => d.seating_plane_id == lid).ToList()

    return new JsonResult { Data = L, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

I call this action from another controller as follows:

layoutsController L = new layoutsController();
JsonResult result = L.getlayouts(lid);

My question is: how can I get data from a result object?

+4
source share
1 answer

Ok, look how you build the object:

new JsonResult { Data = L, JsonRequestBehavior = JsonRequestBehavior.AllowGet }

You set a variable Lto a property with a name Data. So just read this property:

List<layouts> L = (List<layouts>)result.Data;

There is nothing special in that this is an action of the MVC controller.

You simply call a method that returns the object that was constructed in the method and reads the properties from that object. Like any other C # code.

+5
source

All Articles