Modal bootstrap opening a redirect page that should not be inside it

Asked

Viewed 71 times

0

Inside the controller of my partial View that are opened in modal I make a validation if the token is valid, in case everything happens well it will open and show the content, and this happens normal, but if the token is expired I programmed it to be redirected to the method of undoing, then the user would be undocked and would have to log in again, what happens and that the modal loads the login page and updating the page it does the process that should be done.

follows the controller:

        [HttpGet]
    public async Task<IActionResult> Partial_Visualizar(int id)
    {
        try
        {
            if (!SessaoExpirada)
            {
                var receita = await _receitaService.VisualizarReceitaId(id, Token);
                if (receita == null)
                {
                    return Json("Erro ao carregar receita!!");
                }
                var receitaMap = _mapper.Map<ReceitaModel>(receita);
                return View(receitaMap);
            }
            return RedirectToAction("Deslogar", "LoginUsuario", new { msg = "Sessao expirada refaça o login" });
        }
        catch (Exception)
        {
            return Json("Erro no servidor!");
        }
    }

follows the scribe who opens the modals:

ShowInModal = (url, title, id) => {
$.ajax({
    type: "GET",
    url: url,
    success: function (res) {
        $("#exampleModal .modal-body").html(res);
        $("#exampleModal .modal-title").html(title);
        $("#exampleModal").modal('show');
    }
});

};

follows . csthml the part that makes the call of modals:

            @foreach (var modelo in Model)
        {
            <tbody>
                <tr class="table-light">
                    <th scope="row">@modelo.Id</th>
                    <td>@modelo.Titulo</td>
                    <td>@modelo.Data_Publicacao</td>
                    <td>
                        <div class="btn-group" role="group" aria-label="Exemplo básico">
                            <a onclick="ShowInModal('@Url.Action("Partial_Visualizar","Perfil",new {id = modelo.Id},Context.Request.Scheme)','Visualizar Receita')"
                                class="visualizar btn btn-info btn-sm">Visualizar</a>
                            <a onclick="ShowInModal('@Url.Action("Partial_Editar","Perfil",new {id = modelo.Id},Context.Request.Scheme)','Editar Receita')"
                                class="editar btn btn-warning btn-sm">Alterar</a>
                            <a onclick="ShowInModal('@Url.Action("Partial_Delete","Perfil",new {id = modelo.Id},Context.Request.Scheme)',null)"
                                class="deletar btn btn-danger btn-sm">Deletar</a>
                        </div>
                    </td>
                </tr>
            </tbody>
        }

Error image that appears on the Chrome console:inserir a descrição da imagem aqui

My doubt is why this redirection is not done and why the login page is open within the modal, what I would have to change for my idea to work?

1 answer

1


As you are making the request from an ajax function in your view, you will get the return there as well. If you give a.log(res) console, note that it says there is a redirect.

I suggest you change the return, for example an unauthorized status code 401.

instead of

    return RedirectToAction("Deslogar", "LoginUsuario", new { msg = "Sessao expirada refaça o login" });

use

  return Unauthorized(); //status code 401 

do something like this in your job

 success: function (res) {
  if(res.statuscode == 401) 
   { 
       window.location.href = '/LoginUsuario/Deslogar';
   }

    $("#exampleModal .modal-body").html(res);
    $("#exampleModal .modal-title").html(title);
    $("#exampleModal").modal('show');
}

You can keep your controller the way it is if you want, but take a look at what’s coming back, and redirect if it exists...

  • Thank you for the reply! I had already solved this but in an ugly way creating a button in the same controller and made a function to update the page. But your answer didn’t work exactly, but doing some research added an error below so Success: error: function () {&#xA; var msg = 'Sessao expirada refaça o login';&#xA; window.location.href =/Loginusuario/Deslogar? msg=${msg};&#xA; } and kept the unathorized you pointed out and now it’s running good vlw man!!

Browser other questions tagged

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