Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - how to make users confirm the delete

Tags:

asp.net-mvc

He, I have this page where i have checkboxes next to every item in a table, and want to allow the user to select some of them and press my delete button. I just cant come up with the jquery for making the confirm window and submitting only if 'yes' is pushed - this is my page

<%Html.BeginForm(); %>
<%List<ShopLandCore.Model.SLGroup> groups = (List<ShopLandCore.Model.SLGroup>)Model; %>
<%Html.RenderPartial("AdminWorkHeader"); %>
<table width="100%" id="ListTable" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="5" class="heading">
            <input type="submit" name="closeall" value="Close selected" />
            <input type="submit" name="deleteall" value="Delete selected" />
        </td>
    </tr>
    <tr>
        <th width="20px">
        </th>
        <th>
            Name
        </th>
        <th>
            Description
        </th>
        <th width="150px">
            Created
        </th>
        <th width="150px">
            Closed
        </th>
    </tr>
    <%foreach (ShopLandCore.Model.SLGroup g in groups)
      { %>
    <tr>
        <td>
            <%=Html.CheckBox(g.Id.ToString()) %>
        </td>
        <td>
            <%=g.Name %>
        </td>
        <td>
            <%=g.Description %>
        </td>
        <td>
            <%=g.Created %>
        </td>
        <td>
            <%=g.Closed %>
        </td>
    </tr>
    <%} %>
</table>
<%Html.EndForm(); %>

Please note that its only for the delete that it should confirm, and not necessarily for the close button.

like image 593
Brian Hvarregaard Avatar asked Dec 08 '22 03:12

Brian Hvarregaard


2 Answers

Simply add the following to the head of your page:

<script type="text/javascript">
    $(document).ready(function(){
        $("input[name='deleteall']").click(function() {
            return confirm('Delete all selected elements?');
        });
    });
</script>
like image 51
richeym Avatar answered Jan 08 '23 03:01

richeym


You should put a javascript onclick method on your button, to display a messagebox and then stop processing the click if the user chooses no.

You can modify your delete button code to:

<input type="submit" name="deleteall" value="Delete selected" onclick="return confirm('Are you sure you want to Delete?');"/>
like image 29
NibblyPig Avatar answered Jan 08 '23 02:01

NibblyPig