I'm doing an error handling using Json Result, when I displayed the error message it should display the formed page as well as it rules in the _Layout file, so the page is loading all white, does anyone know how to solve it? Below the image of how it is being displayed!
HereismyhomeController:
usingReportandoErro.Models;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;namespaceReportandoErro.Controllers{publicclassHomeController:Controller{publicActionResultIndex(){returnView();}publicActionResultTeste(){varresponse=newResponseViewModel();try{thrownewException("Oops, ocorreu um erro");
}
catch (Exception e)
{
return ErroCapturado(e);
}
return Json(response, JsonRequestBehavior.AllowGet);
}
public ActionResult ErroCapturado(Exception ex)
{
var response = new ResponseViewModel
{
Data = ex.Data,
Sucesso = false,
Mensagem = ex.Message
};
return Json(response, JsonRequestBehavior.AllowGet);
}
}
}
Here's my AccountViewModels:
public class ResponseViewModel
{
public object Data { get; set; }
public bool Sucesso { get; set; }
public string Mensagem { get; set; }
}
And here's the test call view:
@{
ViewBag.Title = "Teste";
}
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model ReportandoErro.Models.ResponseViewModel
<h2>Teste</h2>
<script type="text/javascript">
$(document).ready(function () {
//debugger;
gerandoRelatorio();
function gerandoRelatorio() {
$.getJSON("Home/Teste", function (response) {
if (response.sucesso) {
console.log(response.data);
}
else {
alert(data.mensagem);
}
}).fail(function (response) {
//Erro genérico
alert("Não foi possível processar a sua requisição");
});
}
});
</script>