Grab ID inserted by Ajax

Asked

Viewed 105 times

1

I have a function that includes a register, and when including I call another function, in this other function I need to pass the last ID included, how can I do this ?

function SalvarHorario() {


    //NomeHorario
    var nome = $("#Nome").val();

    var token = $('input[name="__RequestVerificationToken"]').val();
    var tokenadr = $('form[action="/Horario/Create"] input[name="__RequestVerificationToken"]').val();
    var headers = {};
    var headersadr = {};
    headers['__RequestVerificationToken'] = token;
    headersadr['__RequestVerificationToken'] = tokenadr;

    //Gravar
    var url = "/Horario/Create";

    $.ajax({
        url: url
        , type: "POST"
        , datatype: "json"
        , headers: headersadr
        , data: { Id: 0, Nome: nome, __RequestVerificationToken: token }
        , success: function (data) {
            //if (data.Resultado > 0) {
            ListarItens(data.Resultado);
            //}
        }
    });
}

This function works, only for the function Listaritens need to pass the idHorario, which has just been included, in all the ways I try, it will null. Follows how List Items works.

function ListarItens(idHorario) {

    var url = "/HorarioItem/Create";

    $.ajax({
        url: url
        , type: "GET"
        , data: { id: idHorario }
        , datatype: "html"
        , success: function (data) {
            var divItens = $("#divItens");
            divItens.empty();
            divItens.show();
            divItens.html(data);
        }
    });

}

I tried idHorario = date, idHorario = date. Id, idHorario = date. By including I already need to pass this id to the other function. I am working with MVC Core. Thank you.

  • Friend you will need to take the insertion of your record and return in the request’s Answer, in sql server is called scope_identity and in mysql is last_inserted_id.

  • I use MVC Core, the Razor CRUD, I already have the page ready, I have to do the function right after include ? Would that be it ? Sorry, I’m new to the language, I’m still catching up

  • The data.Resultado is coming empty? Try to give a console.log(data.Resultado) and see if the result is actually coming in the function, coming from the server.

  • Yes, is coming Undefined ..

  • So it’s not getting what you really need. Do console.log(data) and see what returns.

  • It returns Undefined, it is saving in the bank with the function, however it does not return the id.

  • You return something at the end of the Create function of the Time Controller?

Show 2 more comments
No answers

Browser other questions tagged

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