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?
-
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).
-
Don't know why I didn't think of that.
Nice and simple - thank you :)
-
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.
Ash : Personally I feel using javascript in this way with MVC is a hack, MVC allows for multiple forms per page (unlike classic asp.net) for a reason.. -
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...
0 comments:
Post a Comment