Hi,
I want to update a record using WebApi and angularJs
following is my html
<div ng-app="myApp">
<div ng-controller="myCtrl" ng-init="GetAllData()">
<p>List Of Employees</p>
<table class="table table-bordered table-hover">
<thead class="thed-dark"> <tr><th scope="col">ID</th><th>FirstName</th><th>LastName</th><th>Company</th><th>Actions</th></tr></thead>
<tr ng-repeat="Emp in employees">
<td>{{Emp.Id}}</td>
<td>{{Emp.FirstName}}</td>
<td>{{Emp.LastName}}</td>
<td>{{Emp.Company}}</td>
<td>
<input type="button" class="btn btn-warning" value="Update" ng-click="UpdateEmp(Emp)" />
</td>
</tr>
</table>
<table>
<tr><td><label class="col-md-4 control-label">FirstName:</label></td><td><input type="text" class="form-control" placeholder="FirstName" ng-model="FirstName"></td></tr>
<tr><td><label class="col-md-4 control-label">LaststName:</label></td><td><input type="text" class="form-control" placeholder="FirstName" ng-model="LastName"></td></tr>
<tr><td><label class="col-md-4 control-label">Company:</label></td><td><input type="text" class="form-control" id="inputEmail" placeholder="Company" ng-model="Company"></td></tr>
<tr><td><input type="button" id="btnSave" class="form-control btn-space" value="Submit" ng-click="UpdateData(Emp)" /></td></tr>
</table>
</div>
</div>
code for displaying in table
$scope.GetAllData = function () {
$http.get('/api/employee').then(function (response) {
$scope.employees = response.data
}, function (status) {
alert(status);
});
}
when I click on the update button of any row ,the details will be displays in textboxes
code for displaying in text box
$scope.UpdateEmp = function (Emp) {
// $scope.employees[index].FirstName = "rajkumar";
$scope.FirstName = Emp.FirstName;
$scope.LastName = Emp.LastName;
$scope.Company = Emp.Company;
}
and finally code for updating
$scope.UpdateData = function (Emp) {
$http.put('/api/Employee',Emp).then(function () {
alert("success");
})
}
web api code for updating
public IHttpActionResult PutEmployee(int id, Employee employee)
{
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
{