Controller returning partial view in a modal

7

I have a button on my screen that held call to a method in my controller (Ajax) that returns a partialView by default.

$("#btn-Visualizar-Rotina").click(function() {
            var codUnimetPcp = '@Model.UnidadeMetalica.COD_UNIMET_PCP';
            $("#modal-Rotina").load("Rotina/Index", { "codUnimetPCP": codUnimetPcp }, function (result) {
                $("#modal-Rotina").modal({
                    backdrop: 'static',
                    keyboard: true
                }, 'show');
            });
        });

However, in my control, I need to re-validate if the user does indeed have permission on the method in question, and if not, derive to a default security page. In the validation function, when the user does not have access, I have the following code snippet

filterContext.Result = new ViewResult
{
    ViewName = "~/Views/AcessoNegado.cshtml",
    ViewData = filterContext.Controller.ViewData,
    TempData = filterContext.Controller.TempData,
};

However, when this happens, my AcessoNegado.cshtml page is not loading. It is being rendered within the modal.

I tried to return a ViewBag indicating that authentication failed to display the modal, but it always comes with null .

I would like your help in solving this problem in the best way possible.

    
asked by anonymous 28.07.2016 / 22:21

1 answer

0

I do not know if you have solved this yet, but a generic way to solve this is to use the Request Headers.

In the Controller of your partialView you do your validations and add the URL of the error page to some Header tags:

HttpContext.Response.AppendHeader("Denied", "url/paginadeerro");

And you can use the jQuery ajaxComplete, ajaxComplete is a function where you can bind all the Ajax executed on the page, so you can validate if the Header exists, and redirect to the error page: / p>

  $(document).ajaxComplete(function (event, xhr, settings) {
    var h = xhr.getResponseHeader("Denied");
    if (h !== undefined && h !== null) {
        alert("Acesso negado!");
        window.location.href = h ;
    }
});
    
12.08.2016 / 14:08