MVC5 - Refresh partial view in ajaxStop

Asked

Viewed 521 times

0

Hello,

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

Code of partial view

@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 also.

My problem is that in some methods I make the call via AJAX, updating only another partial view with the result of the search for example. In this scenario, as the partial message view 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 event "ajaxStop" a chunk that code that causes the partial message view to be loaded again, but I’m not sure how to do this.

I fight again with your help.

1 answer

1


Controller

Create an action that returns your "Partial View".

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

Javascript

Creates a function that loads within a div the content of the above action. I have never used ajaxStop, would put in the success or in the error callbacks. But I believe it’s the same thing 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")');
}

Browser other questions tagged

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