Delete record using Web API in ASP.NET MVC

How to delete record from database by consuming ASP.NET Web API from ASP.NET MVC View?

Please refer to the previous post of Listing records using Web API that has list of records and delete link corresponding to each record. Clicking on the delete link fires below method.
DELETING RECORD
Notice the previous post UI where the last column is Delete link. Clicking on Delete link calls DeleteRecord() function with AutoId parameter. In the DeleteRecord method, we call DeletePersonalDetails action method
CONTROLLER ACION METHOD
// DELETE api/PersonalDetails/5
[HttpDelete]
public IHttpActionResult DeletePersonalDetails(int id)
{
    PersonalDetails personaldetails = db.PersonalDetails.Find(id);
    if (personaldetails == null)
    {
        return NotFound();
    }

    db.PersonalDetails.Remove(personaldetails);
    db.SaveChanges();

    return Ok(personaldetails);
}
In this action method, first we find the record to delete based on Id and then delete that record from the database. This method also returns Ok with the record deleted that shows Success message and relist all records by calling LoadAll() javascript function.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...