Show success message using jQuery AJAX after form submission in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

In a post of a webform application asp.net 4.0

i save a user order then in case of success i would add a div with success

but i can't 

$.ajax({
    url: 'api/Order',
    data: JSON.stringify(order),
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    beforeSend: function () {
      ...
    },
    success: function (result) {
        $('#message').append("<div class='alert alert-success'><i class='fa fa-check'></i> Order successfully added!</div>");                   
    },
    complete: function () {
        $('#myModal').modal('hide');
    },
    failure: function (jqXHR, textStatus, errorThrown) {
       ...
    }
});

 

    public void Post([FromBody] ComponibileCreate model)
    {
        if (!ModelState.IsValid) return;
        string utente = HttpContext.Current.User.Identity.Name;       
        var taskWork = Task.Run(() => SaveOnDatabase(model, utente));
        //Do other work...
        var codici = String.Join("-", model.Righe.Select(i => i.Codice));
        var simboli = String.Join("*", model.Righe.Select(i => i.Descrizione));

        SendMail(utente, codici, userMail);
        SendMailToUser(utente, ragsoc, simboli, userMail);

        taskWork.Wait();//Then wait for thread finish
    }

    private void SaveOnDatabase(ComponibileCreate model, string utente)
    {
        var ordine = new COM_ORDINI
        {
            Data = DateTime.Now,
            Codcli = utente,
            Totale = model.Totale
        };

        foreach (var item in model.Righe)
        {
            var righe = new COM_RIGHE
            {
                COM_ORDINI = ordine,
                Codice = item.Codice,
                Descrizione = item.Descrizione,
                Prezzo = item.Prezzo
            };
            ordine.COM_RIGHE.Add(righe);
        }

        _ctx.COM_ORDINI.Add(ordine);
        _ctx.SaveChanges();
    }

I recive no error on console and in database all works

Posted one year ago

Hi jon,

What is the return type of your Order method.

Ifreturn type is void then remove below line from ajax call.

dataType: 'json',

Else please share the Order method.