How To Show Confirm Alert Box In ASP.NET MVC Before Deleting Record

In this article, we will learn how to show the confirm alert box before deleting or ensuring any user action. Let's consider we have the following html table which has a list of records.
<table class="table">
 <tr>
 <th>
 @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.City)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)

    {
        @Html.HiddenFor(model => item.Empid)
        <tr>
           <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
              @Html.ActionLink("Edit", "EditEmpDetails", new { id = item.Empid }) |
               @Html.ActionLink("Delete", "DeleteEmp", new { id = item.Empid }
               )
         </td>
        </tr>
  }
</table>
We don't need a JavaScript or jQuery function to show the alert box, you just need to modify the ActionLink in view. The steps are as follows.
@Html.ActionLink("Delete", "DeleteEmp", new { id = item.Empid },
new { onclick = "return confirm('Are sure wants to delete?');" })
In the above ActionLink
  • Delete is the ActionLink Name.  
  • DeleteEmp is the ActionResult name .
  • EmpId is the unquie id from which specific records are deleted. 
After clicking on the delete button, the alert box will be shown as follows.


I hope from all the preceding examples you have learned how to show an alert box Action Link click in ASP.NET MVC.

Related articles

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode