5

I have the following Action in my layouts Controller

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 am calling this Action from another controller like so:

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

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

Geoff James
  • 3,122
  • 1
  • 17
  • 36
rakshith
  • 704
  • 2
  • 10
  • 23

2 Answers2

6

Well, have a look how you're building the object:

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

You're setting the L variable to a property called Data. So just read that property:

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

There's nothing special about the fact that it's an MVC controller action.

You're simply calling a method which returns an object that was constructed in the method, and reading properties from that object. Just like any other C# code.

Geoff James
  • 3,122
  • 1
  • 17
  • 36
David
  • 208,112
  • 36
  • 198
  • 279
  • thank u sir. i m stuck in retriving the jsonresult data.actually i dont know how to itrate json data inside c sharp. – rakshith May 13 '16 at 11:35
  • 1
    @rakshi As a suggestion, try converting your Json data to C# objects - http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object – Geoff James May 13 '16 at 11:38
  • 1
    @rakshi: Why are you using JSON here in the first place? If you're invoking code directly in the same application, just use your C# objects. There doesn't seem to be a need for this. – David May 13 '16 at 11:40
  • 2
    thank u...i have used the following code and worked for me List L1 = (List)result.Data; String lid1 = L1[0].ticket_no_start; int lid= Int32.Parse(lid1); – rakshith May 14 '16 at 06:03
1

I have my class:

public class ResponseJson
{
    public string message { get; set; }
    public bool success { get; set; }
}

in my method SendEmail

private async Task<JsonResult> SendEmailAsync(ApplicationUser user, string returnUrl, string empleadoNombre, string pwdInic)

i will return my JsonResult

ResponseJson response = new ResponseJson();

response.success = true;
response.message = "Operación exitosa";

return new JsonResult( response);

to read the result returned from my SendEmail method

JsonResult emailSend = await SendEmailAsync(user, returnUrl, empleadoNombre, pwdInic);
ResponseJson response = new ResponseJson();
                
try
{ 
      string json = JsonConvert.SerializeObject(emailSend.Value);
      response = JsonConvert.DeserializeObject<ResponseJson>(json);

}
catch(Exception e)
{

}

emailSend

response