In this article I will explain with an example, how to display Confirmation Box before Delete in ASP.Net MVC Razor.
The Delete Button in WebGrid Row will be assigned JavaScript Confirm function, so that when the Delete Button is clicked, a JavaScript Confirmation Box will be displayed in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Display Confirmation Box before Delete in ASP.Net MVC
I have already inserted few records in the table.
Display Confirmation Box before Delete in ASP.Net MVC
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Display Confirmation Box before Delete in ASP.Net MVC
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, all the records from the Customers table is returned to the View as a Generic List collection.
Action method for Deleting
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.
Once the record is referenced, the Customer record is deleted from the Customers table.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        CustomersEntities entities = new CustomersEntities();
        List<Customer> customers = entities.Customers.ToList();
        return View(customers.ToList());
    public ActionResult DeleteCustomer(int customerId)
        using (CustomersEntities entities = new CustomersEntities())
            Customer customer = (from c in entities.Customers
                                 where c.CustomerId == customerId
                                 select c).FirstOrDefault();
        return new EmptyResult();
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that the Model will be available as a Collection.
The WebGrid is initialized with the Model i.e. IEnumerable collection of Customer Entity class objects as source.
Note: For more information on usage of WebGrid, please refer WebGrid Step By Step Tutorial with example in ASP.Net MVC.
When the Delete Button is clicked, the reference of the HTML Table row is determined and the value of the CustomerId is fetched and passed to the DeleteCustomer Action method using jQuery AJAX call.
Once the response is received the respective row is removed from the HTML Table row.
@model IEnumerable<Customer>
    Layout = null;
    WebGrid webGrid = new WebGrid(source: Model, canPage: true, canSort: false);
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        .Grid {
            border: 1px solid #ccc;
            border-collapse: collapse;
        .Grid th {
            background-color: #F7F7F7;
            font-weight: bold;
        .Grid th, .Grid td {
            padding: 5px;
            width: 110px;
            border: 1px solid #ccc;
        .Grid, .Grid table td {
            border: 0px solid #ccc;
        .Grid th a, .Grid th a:visited {
            color: #333;
        htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
        columns: webGrid.Columns(
                 webGrid.Column(header: "Customer Id", format: @<span class="label">@item.CustomerId</span>, style: "CustomerId" ),
                 webGrid.Column(header: "Name", format: @<span><span class="label">@item.Name</span>
                            <input class="text" type="text" value="@item.Name" style="display:none"/></span>, style: "Name"),
                 webGrid.Column(header: "Country", format: @<span><span class="label">@item.Country</span>
                            <input class="text" type="text" value="@item.Country" style="display:none"/></span>, style: "Country"),
                 webGrid.Column(format:@<span class="link">
                        <a class="Delete" href="javascript:;">Delete</a>
                    </span> )))
    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        //Delete event handler.
        $("body").on("click", "#WebGrid TBODY .Delete", function () {
            if (confirm("Do you want to delete this row?")) {
                var row = $(this).closest("tr");
                var customerId = row.find(".label").html();
                    type: "POST",
                    url: "/Home/DeleteCustomer",
                    data: '{customerId: ' + customerId + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        if ($("#WebGrid TBODY tr").length == 1) {
                        } else {
Display Confirmation Box before Delete in ASP.Net MVC