update not working in webapi

Posted by Klbaiju under ASP.NET Web API on 8/3/2018 | Points: 10 | Views : 2787 | Status : [Member] | Replies : 1
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
{





Responses

Posted by: Denisbill on: 8/6/2018 [Member] Starter | Points: 25

Up
0
Down
We are providing all the solution of Canon Printer Errors. If you are having any issue with your canon printer the visit our website to get the best explanation of your problems related to canon printers.
https://www.printererrorrepair.com/blog/fix-canon-printers-error-100-250-0-0/

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response