MVC5 - Refresh partial view on ajaxStop

0

Hello,

Inside my main layout page (_Layout), I render a partial view that is responsible for displaying messages that have been added by the controlled, through the Toastr javascript library.

Partial view code

@using Biblioteca.Util
@using Biblioteca.Util.Base.UI

@if (TempData.ContainsKey("Alerta"))
{
   Alerta alerta = TempData["Alerta"] as Alerta;
   @MensagemAlerta.MostrarMensagensAlerta(alerta);
}

When the methods of my pages are executed through POST in the form, the messages are displayed correctly, once the page is reloaded and consequently the partial view as well.

My problem is that in some methods I make the call via AJAX, updating just another partial view with the search result for example. In this scenario, because the partial view of messages is not reloaded, it is not displayed to the user. This occurs only when the user refreshes the page (F5).

My initial idea is to add in the "ajaxStop" event a code that causes the partial view of messages to be loaded again, but I'm not sure how to do that.

Control again with your help.

    
asked by anonymous 12.09.2016 / 20:29

1 answer

1

Controller

Create an action that returns your "Partial View".

    public ActionResult Mensagens()
    {
        return PartialView("_ViewParcialMensagens");
    }

Javascript

Create a function that loads the contents of the above action into a div. I've never used ajaxStop , would put success or error callbacks. But I believe it's the same for ajaxStop

function salvar() {
    var dados = $("#form").serialize();
    $.ajax({
        type: "POST",
        url: '@Url.Action("MinhaAcao", "MeuController")',
        data: { dados },
        dataType: 'json',
        success: function (data) {
        },
        ajaxStop: function () {
            exibirMensagens();
        }
    });
}

function exibirMensagens() {
   $("#div-mensagens").load('@Url.Action("Mensagens", "MeuControler")');
}
    
12.09.2016 / 20:52