ASP.Net WebAPI Post Method在数据库中存储空值

时间:2017-03-08 01:01:13

标签: c# jquery asp.net-ajax

这是我的HTTP GET请求方法,它采用数据表单数据库并以无序列表显示

 $("#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请求方法工作正常,但这是我的POST请求方法,它将空值发布到数据库中。

$(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");
                        }
                    });
                });
                });

数据库中的输出enter image description here 这是在控制器内处理post请求的方法:

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

以下是Employee Class的定义

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

1 个答案:

答案 0 :(得分:0)

我得到了答案。我在Post请求中做了一些更改并且它有效:

 $(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");
                    }
                });
            });
            });

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