简体   繁体   中英

ASP.Net WebAPI Post Method stores null value in the database

This is my HTTP GET request method that takes the data form database and displays in unordered list

 $("#ulEmployees").ready(function () 
            {
            var ulEmployees = $('#ulEmployees');
            $("#btn").click(function () {
                $.ajax({
                    type: 'GET',
                    url: 'api/Employees',
                    dataType: 'json',
                    success: function (data) {
                        ulEmployees.empty();
                        $.each(data, function (index, val) {
                            var info = val.Employee_Name + ' Works for ' + val.Employee_Department + ' Department.';
                            ulEmployees.append('<li>' + info + '</li>')


                        });
                    }
                });
            });
        });

GET request method works fine but here is my POST request method that posts null value into the database.

$(document).ready(function(){
                $("#insertbutton").click(function () {
                    //var emp = new Object();
                    var name = $("#employeename").val();
                    var dep = $("#insertbutton").val();

                    $.ajax({
                        url: 'api/Employees',
                        type: 'POST',
                        dataType: 'json',
                        // data: emp,
                        data:'{"Employee_Name":"' +name+'","Employee_Department": "' +dep+'"}',
                        success: function () {
                            alert("Poduct is Inserted Successfully");
                        }
                    });
                });
                });

Output in the database enter image description here Here is method to handle post request inside controller:

public void Post(Employee emp)
        {
            CompanyEntities ent = new CompanyEntities();
            ent.Employees.Add(emp);
            ent.SaveChanges();
        }

Here is the definition of Employee Class

 public partial class Employee
    {
        public int Employee_Id { get; set; }
        public string Employee_Name { get; set; }
        public string Employee_Department { get; set; }
    }
}

I got the answer. I made some changes inside the Post request and it worked:

 $(document).ready(function(){
            $("#insertbutton").click(function () {
                //var emp = new Object();
                var name = $("#employeename").val();
                var dep = $("#insertbutton").val();
                // console.log(name);

                var sendinfo = {
                    Employee_Name: name,
                    Employee_Department: dep

                };

                $.ajax({
                    url: 'api/Employees',
                    type: 'POST',
                    dataType: 'json',
                    // data: emp,
                    data:sendinfo,
                    success: function () {
                        alert("Employee Inserted Successfully");
                    }
                });
            });
            });

Reference: Send JSON data via POST (ajax) and receive json response from Controller (MVC)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM