Reload page with MVC

3

I made a function in the model and the call in the controller. There, I move to jquery and the command to write to DB is successfully executed. It happens, that when I finish recording, the screen continues with the previous information, that is, the textbox filled, the checkbox's checked. How do I do after recording in BD (ajax success) I reload the page or changed fields only? Below my jquery call. The alert with the message can be disregarded, if necessary.

var checkedItemsUn = {}, checkedItemsMot = {}, checkedItemsPdv = {}, counter = 0;

function Gravar() {

    $("#check-list-box li.active").each(function (idx, li) {
        checkedItemsUn[counter] = $(li).text();
        counter++;
    });

    counter = 0

    $("#check-list-box-mot li.active").each(function (idx, li) {
        checkedItemsMot[counter] = $(li).text();
        counter++;
    });

    counter = 0

    $("#check-list-tipo li.active").each(function (idx, li) {
        checkedItemsPdv[counter] = $(li).text();
        counter++;
    });

    counter = 0
    str = "";

    $.ajax({
        url: '/CadastroCargo/GravaResponsavel',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
        data: JSON.stringify({ _responsavel: $('#txtResponsavel').val(), _ativo: $('#ckbAtivo').prop("checked"), _Un: checkedItemsUn,
            _motivo: checkedItemsMot, _pdv: checkedItemsPdv, _nivel: $('#cbxNivelResp').val()
        }),
        success: function (data) {

            str += '<div class="alert alert-success col-md-6">';
            str += '<button type="button" class="close" data-dismiss="alert"></button>';
            str += '<h4>Operação realizada com sucesso!</h4>';
            str += 'Registro gravado no banco de dados com sucesso.';
            str += '</div>';

            $('#alerta').html(str);

            str = "";
        },
        error: function (error) {
        }
    })
}
    
asked by anonymous 08.10.2014 / 14:15

1 answer

5

If you want to load a view again, you can use window.location.href and success of your Ajax request.

As such, it will look something like this:

$.ajax({
        url: '/CadastroCargo/GravaResponsavel',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
        data: JSON.stringify({ _responsavel: $('#txtResponsavel').val(), _ativo: $('#ckbAtivo').prop("checked"), _Un: checkedItemsUn,
            _motivo: checkedItemsMot, _pdv: checkedItemsPdv, _nivel: $('#cbxNivelResp').val()
        }),
        success: function (data) {

            str += '<div class="alert alert-success col-md-6">';
            str += '<button type="button" class="close" data-dismiss="alert"></button>';
            str += '<h4>Operação realizada com sucesso!</h4>';
            str += 'Registro gravado no banco de dados com sucesso.';
            str += '</div>';

            $('#alerta').html(str);

            str = "";

            window.location.href = '/CadastroCargo/CadastroCargo' //AQUI
        },
        error: function (error) {
        }

Where in the CadastroCargo function you load the data to load in the view you want.

    
08.10.2014 / 16:03