Brian Hvarregaard
Brian Hvarregaard

Reputation: 4209

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

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.

Upvotes: 4

Views: 14499

Answers (3)

richeym
richeym

Reputation: 4089

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>

Upvotes: 6

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

Here's how to register a click event to your button unobtrusively using jQuery (without mixing html markup and script logic):

$(function() {
    $('input[name=deleteall]').click(function() {
        return confirm('Are you sure');
    });
});

Upvotes: 4

NibblyPig
NibblyPig

Reputation: 52952

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?');"/>

Upvotes: 5

Related Questions