Show Confirm Alert Box on ActionLink Click In ASP.NET MVC

Many times we need show confirm alert box while deleting ,saving and editing information in MVC application .The beauty of MVC is pure html and also records are bound using Using HTML table tag and action link is placed Table list to delete ,Edit the details . so before deleting any records we need to show the confirm box ,so let us see how we can achieve it without writing much javascript or JQuery code .

Let us consider following is the Table list details .

<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>
Now to show alert box just modify the ActionLink in view 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 delete button the alert box will be shown are as follows


Summary
I hope this article is useful for all readers. If you have any suggestion then please contact me..

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode