Controller returning partial view in a modal

Asked

Viewed 805 times

8

I have a button on my screen that held call to a method on 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 validate again if the user really has permission in the method in question, and in the negative case derive to a standard 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 situation occurs, my page AcessoNegado.cshtml is not being loaded. It is being rendered inside the modal.

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

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

  • There is a problem in its formulation. Depending on the result of Ajax, the screen requires a state transfer. That is, your Ajax should not return a ViewResult. You need to decide whether logic has to transfer the state of the screen or not. If it is not, the correct thing is to display a JS message explaining the lack of access.

  • In fact, redirecting to a new page is a definition of the application scope... Not being Viewresult, do you have any idea what I can return? Or it is necessary to make a change in the form in which the controller is being called and consequently the Modal?

  • For functions that do not return partialView and are not opened in modal, the redirect works correctly.

  • From what I understand, the result of the modal should be a JSON containing some business rule code. If there is no permission, the JS must transfer the screen to the unauthorized screen.

  • I was trying to return some indication after the call Ajax so that the event "load" modal was not executed, preventing the page "without permission" was opened inside the modal

  • I was thinking of a way that when the controller redirects, the event that opens the modal would not be executed...

Show 1 more comment

1 answer

1

I don’t know if you’ve solved this yet, but one generic way to resolve this issue is by using the Request Headers.

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

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.