In this article I will explain with an example, how to delete data from Database using jQuery AJAX and Entity Framework in ASP.Net MVC Razor.
When the Delete Button is clicked, the DeleteCustomer Action method of the Controller is called using jQuery AJAX and the details of Customer are sent as JSON object.
Finally, a Customer object is sent to the View which indicates that the record was successfully deleted or not.
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Controller
The Controller consists of the following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for Deleting data from Database
Inside this Action method, the CustomerId value is received as parameter. The CustomerId value is used to reference the Customer record in the Customer Entities.
If the record is found, the Customer record is deleted from the Customers table and the Customer object is returned.
If the record is not found, then an EmptyResult class object is returned.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult DeleteCustomer(Customer _customer)
{
using (CustomersEntities entities = new CustomersEntities())
{
Customer customer = (from c in entities.Customers
where c.CustomerId == _customer.CustomerId
select c).FirstOrDefault();
if (customer != null)
{
entities.Customers.Remove(customer);
entities.SaveChanges();
return Json(customer);
}
}
return new EmptyResult();
}
}
View
The View consists of a TextBox and a Button. The Button has been assigned with a jQuery Click event handler.
When the Delete button is clicked, the value of CustomerId is passed to the DeleteCustomer Action method using jQuery AJAX call.
Once the response is received, based on whether response is a Customer object or NULL object, appropriate message is displayed using JavaScript Alert Message Box.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 150px">
Customer Id:<br/>
<input type="text" id="txtCustomerId" style="width:140px"/>
</td>
<td style="width: 200px">
<br/>
<input type="button" id="btnDelete" value="Delete"/>
</td>
</tr>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script type="text/javascript">
$("body").on("click", "#btnDelete", function () {
if (confirm("Do you want to delete this Customer?")) {
var _customer = {};
_customer.CustomerId = $("#txtCustomerId").val();
$.ajax({
type: "POST",
url: "/Home/DeleteCustomer",
data: JSON.stringify(_customer),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (customer) {
if (customer != null) {
alert("Deleted Customer: " + customer.Name);
} else {
alert("Customer record not found.");
}
}
});
}
});
</script>
</body>
</html>
Screenshot
Downloads