After Ajax Function, refresh the page

Asked

Viewed 717 times

0

I have this function, and I would like it to work everything ok, I would like you to update the page, but it doesn’t always update, follows how I am doing:

function GravarDados() {
    var dataInicio = $("#txtDataInicio").val();
    var dataFim = $("#txtDataFim").val();
    var diaVencimento = $("#txtDiaVencimento").val();
    var tolerancia = $("#txtTolerancia").val();
    var valor = $("#txtValor").val();
    var planoId = $("#cbplanos option:selected").val();
    var tipoPlano = $("#txtTipoPlano").val();
    var pessoaId = $("#id").val();
    var proporcional = $("#lblProporcional").val();
    var dataTolerancia = $("#txtDataTolerancia").val();
    var vencimentoC = $("#txtVencimentoC").val();
    var descricao = $("#cbplanos option:selected").text();
    var pre = $('#cbpre').prop('checked');
    var pos = $('#cbpos').prop('checked');
    var pro = $('#cbproporcional').prop('checked');
    var url = "/PessoasServicos/Gravar";

    $.ajax({
        url: url
        , data: { DataInicio: dataInicio, DataFim: dataFim, DiaVencimento: diaVencimento, Tolerancia: tolerancia, Valor: valor, PlanoId: planoId, TipoPlano: tipoPlano, PessoaId: pessoaId, Descricao: descricao, Proporcional: proporcional, Pre: pre, Pos: pos, Pro: pro, DataTolerancia: dataTolerancia, VencimentoC: vencimentoC}
        , type: "POST"
        , datatype: "html"
        , success: function (data) {
            if (data.resultado > 0) {
                location.reload();
            }
        }
    });
}

I call this function here :

 function closeModal() {
            GravarDados();
            $('#myModalPlanos').modal('hide');
            $('body').removeClass('modal-open');
            $('.modal-backdrop').remove();
            location.reload();
        }

And this is where I call closeModal()

<a href="#" onclick="closeModal();" class="btn btn-primary btn-sm">Gravar Item</a>

I have debugged several times, and it goes through all functions, no error, even it enters this, just does not load the data.

  public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            PessoaVM = new PessoasViewModel
            {
                Pessoas = _context.Pessoas.Include(m => m.Classificacoes).SingleOrDefault(m => m.Id == id),
                ClassificacoesVM = _context.Classificacoes.ToList(),
                EstadoCivilVM = _context.EstadosCivies.ToList()

            };

            PessoasServicos = await _context.PessoasServicos
                .Include(m => m.PlanosServicos).Where(m => m.PessoaId == id)
                .ToListAsync();

            ContasReceber = await _context.ContasReceber
                .Include(c => c.Pessoas)
                .Include(c => c.PlanosServicos).Where(c => c.PessoaId == id)
                .ToListAsync();

            if (PessoaVM == null)
            {
                return NotFound();
            }
            return Page();
        }

  • If "not always" updates it is because it gave error. Check the browser console.

  • It does not return error, I have already debugged and even so, I will put the function it enters when loading the page.

  • "not always" updates because you are applying the condition data.resultado > 0 if the result is different will not give Reload.

  • @marianac_costa data.resultado > 0 is to verify that the request was successful?

  • That, I already did the test without the date.result > 0, but the same problem occurs, it does not update, I added the code that it traverses when the page is loaded.

  • True, it must be what Leonardo said. And also: it would be even case to use ajax, since you want to reload the page?

  • It does not update the two tables that need to be updated.

  • I even took the date.result>0 to ensure that the problem was not there, including debugging the code, it enters the condition.

  • sometimes you have to wait for the return of the ajax call so instead of doing the if (date.result > 0) return the date and at the end of the function use the }). done(Function() ... to execute your logic

  • @Marconciliosouza hours works and hours don’t, I already took until that date.result > 0, I do the debugging works perfectly, without any problem, will be something with ASP.NET CORE?

Show 5 more comments
No answers

Browser other questions tagged

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