如何检索JsonResult数据

时间:2016-05-13 11:25:14

标签: c# asp.net-mvc

我的布局控制器

中有以下操作
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 };
}

我从另一个控制器调用此Action,如下所示:

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

我的问题是:如何从结果对象中获取数据?

2 个答案:

答案 0 :(得分:6)

好吧,看看你是如何构建对象的:

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

您正在将L变量设置为名为Data的属性。所以,请阅读该属性:

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

它是一个MVC控制器动作这一事实并不特别。

您只需调用一个方法,该方法返回在方法中构造的对象,并从该对象中读取属性。就像任何其他C#代码一样。

答案 1 :(得分:0)

我上课了

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

以我的方法 SendEmail

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

我将返回我的 JsonResult

ResponseJson response = new ResponseJson();

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

return new JsonResult( response);

读取我的 SendEmail 方法返回的结果

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

相关问题