Inserting Record in DB using ANgular Ajax,webapi
WEB API insert Method
===================================================
// POST: api/Employee
[ResponseType(typeof(Employee))]
public IHttpActionResult PostEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Employees.Add(employee);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (EmployeeExists(employee.Id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = employee.Id }, employee);
}
======================================================
angualrjs Code
======================================================
var app = angular.module("myApp", [])
app.controller("myCtrl", function ($scope, $http) {
$scope.EmployeeInsert = function (employee) {
$http.post("http://localhost:43468/api/Employee", employee).then(function (response) { alert("successfull inserted"); GetEmps(); }, function () { alert("Failed to Insert Record") })
}
}
=====================================================
HTML CODE
++++++++++++++++++++++++=============================
<h1>Insert Employee</h1>
<table>
<tr>
<td>Employee Id</td>
<td><input type="text" ng-model="Emp.Id" /></td>
</tr>
<tr>
<td>Employee Name</td>
<td><input type="text" ng-model="Emp.Name" /></td>
</tr>
<tr>
<td>Employee Gender</td>
<td><input type="text" ng-model="Emp.Gender" /></td>
</tr>
<tr>
<td>Employee Salary</td>
<td><input type="text" ng-model="Emp.salary" /></td>
</tr>
<tr>
<td>
<input type="button" ng-click="EmployeeInsert(Emp)" value="Insert Record">
</td>
</tr>
</table>
================================================================================================================================================Update Record
====================================
html
======
<input type="button" ng-click="EmployeeUpdate(Emp)" value="Update Record">
=========
angular Code
==========
$scope.EmployeeInsert=function(employee)
{
$http.post("http://localhost:43468/api/Employee", employee).then(function (response) { alert("successfull inserted"); GetEmps();}, function () { alert("Failed to Insert Record") })
}
API CODE
=================
// PUT: api/Employee/5
[ResponseType(typeof(void))]
public IHttpActionResult PutEmployee(Employee employee)
{
int id = employee.Id;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != employee.Id)
{
return BadRequest();
}
db.Entry(employee).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
=======================================================
To Get ALL Records
+++++++++++++++++++++++++++++++++++++++++++++++++++++
API COde
=======
// GET: api/Employee
public IQueryable<Employee> GetEmployees()
{
return db.Employees;
}
HTML CODE
============
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Gender</td>
<td>Salary</td>
</tr>
<tr ng-repeat="item in Employees">
<td>{{item.Id}}</td>
<td>{{item.Name}}</td>
<td>{{item.Gender}}</td>
<td>{{item.salary}}</td>
</tr>
</table>
=======================================
AngularCode
GetEmps();
function GetEmps()
{
$http.get("http://localhost:43468/api/Employee").then(function(response){$scope.Employees=response.data},function(){alert("Failed to get data")})
}