Peter Lindholm
Peter Lindholm

Reputation: 2530

How should I handle delete-situations in ASP.NET MVC

How should I initiate a delete action from my view?

Creating a new form-tag for each entity just doesn't seem right :-)

<% foreach (var subscriber in group.Subscribers) { %>
        <tr>
            <td><%= subscriber.Email %></td>
            <td><%= Html.ActionLink("[edit]", "edit", "subscriber", new {id=subscriber.SubscriberId}, null) %></td>
            <td>
                <form id="delete-subscriber-form" method="post" action="<%= Url.Action( "delete", "subscriber", new { @subscriberId = subscriber.SubscriberId }) %>">
                    <input type="submit" value="Delete" />
                </form>
            </td>
        </tr>
        <% } %>

How would you do it?

Upvotes: 2

Views: 1368

Answers (3)

Ash
Ash

Reputation: 5087

It depends on the situation, if you are doing CRUD operations you would normally go with one <form> tag per operation (delete,edit,new). If, however, you are displaying a list and you want to be able to 'multiple delete' items with one click then you will have to approach it from a different angle as you need to encapsulate all the required information into one form.

EDIT

Having had another look at your post above I notice you are providing a 'Delete Button' against each element in the list. For atomic actions like this (i.e. the user expects something to happen straight after they have clicked a button) I would definitely use one form per item.

What I wrote above still applies...

Upvotes: 0

Moran Helman
Moran Helman

Reputation: 18572

you can use CSS and Javascript , add 'forDel' css class for all the elments you want to delete , if you are going to use jquery you can do it like this:

    $(".element").each(function(){
      $(this).data("id","the id of the element in db")
    });

    $(".element").toggle(function(){
      $(this).addClass("forDel");
    },function(){
   $(this).removeClass("forDel");
   });

and then on pressing the delete button:

var idsForDel;
    $(".forDel").each(function(){
   idsForDel = $(this).data("id"); + ";";
})

you can pass the idsForDel to the Controller ... and split it in the server side.

Upvotes: 0

Ot&#225;vio D&#233;cio
Ot&#225;vio D&#233;cio

Reputation: 74320

I normally use checkboxes on the side of the items. Then I can have action links (buttons, whatever) that apply an action to the selected items (such as delete).

Upvotes: 5

Related Questions