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; }
}
}