How to send ajax call to server without expecting a return result?

10,412

Solution 1

Just add the url & data attribute in ajax request.

$.ajax({
  url: "yourURL",
  data: yourData
});

Solution 2

Return void in your action method.

[HttpPost]
public void AddNewContactList(ContactList c)
{
    DbContext m = new DbContext();
    m.ContactList.Add(c);
    m.SaveChanges();
}
Share:
10,412
Mazen Kamal
Author by

Mazen Kamal

Updated on June 04, 2022

Comments

  • Mazen Kamal
    Mazen Kamal almost 2 years

    I pass inputs to actionResult which will use it to create a new object and insert it to database using entityFramework and dont want to return anything.

        $(document).ready(function () {  
            $("#Add").click(function () {
                var EmployeeeId = $("#EmployeeId").val();
                var Phone =$("#Phone").val();
                var Email = $("#Email").val();
                var Address = $("#Address").val(); 
            var UserModel =
    {
        "EmployeeId": EmployeeeId,
        "Phone": Phone,
        "Address": Address,
        "Email": Email
    
    };    $.ajax({url: '@Url.Action("AddNewContactList","Home")',
    
                type: "POST",
                dataType: "json",
    
                data: JSON.stringify(UserModel),
                contentType: 'application/json; charset=utf-8',
    
    
                            success: function (result) {
    
                               //Doesnt Matter
    
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
    
                                alert("Failed");
                            }
    

    And here is my Controller Code with the controller action being called

     [HttpPost]
        public ActionResult AddNewContactList(ContactList c)
        {
            DbContext m = new DbContext();
            m.ContactList.Add(c);
            m.SaveChanges();
            return View();
    }