Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve JsonResult data

Tags:

c#

asp.net-mvc

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?

like image 744
rakshithrai Avatar asked May 13 '16 11:05

rakshithrai


People also ask

What does JsonResult return?

For example, returning JsonResult returns JSON-formatted data. Returning ContentResult or a string returns plain-text-formatted string data.

How do I restore JSON?

Any JSON data can be consumed from different sources like a local JSON file by fetching the data using an API call. After getting a response from the server, you need to render its value. You can use local JSON files to do an app config, such as API URL management based on a server environment like dev, QA, or prod.

How do I return JSON data to view?

How do I return JSON data in view? The Controller Action method will be called using jQuery POST function and JSON data will be returned back to the View using JsonResult class object.

Can Actionresult return JSON?

JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format).


2 Answers

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.

like image 71
David Avatar answered Sep 25 '22 15:09

David


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

like image 30
Matías Gallegos Avatar answered Sep 23 '22 15:09

Matías Gallegos