1

I have a form to delete a record. One the form I am using a delete button and when the button is clicked the form is submitted.

<input type="submit" value="Delete" />

How can I implement a cancel and check in the action if it is the delete or the cancel that has been clicked?

tereško
  • 58,060
  • 25
  • 98
  • 150
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

5 Answers5

8

You have to give the input a name. For example:

<input type='submit' value='Delete' name='action' />
<input type='submit' value='Cancel' name='action' />

And then in your Action:

[HttpPost]
public ActionResult Submit(string action) {
    if (action == "Delete") {
        // User clicked "Delete"
    } else {
        // User clicked "Cancel"
    }
}
Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
  • 1
    Also to note, a "Cancel" button is usually unnecessary for the Web ... a "Back" link makes much more sense. The only case where "Cancel" might make sense is if you have a multi-page form that doesn't save changes until the end, so "Cancel" will indicate that all changes will be discarded. – Scott Rippey Dec 09 '11 at 17:17
2

Do you really need to handle a cancel click event in your controller? You are probably just redirecting the user to a different page when they cancel, right? Javascript has worked pretty well for me:

<input type="button" value="Cancel" 
   onclick="window.location='<%: Url.Action("Details", new { id = Model.Id }) %>'" />
hawkke
  • 4,242
  • 1
  • 27
  • 23
  • +1 That's what I would do or include confirm dialog box on the event listener depending on whether or not there is the need for redirect. `onclick="if (confirm('Are you sure you want to delete your comment?')) $.del(@comment.ArticleId, @comment.CommentId, @comment.UserId ); return false">` – yardpenalty.com Mar 14 '14 at 00:48
0

Using jQuery (well, you could do it in just JavaScript too), you could do something like:

$('input[type="submit"]').click(function(e) {
    if(confirm('Are you sure?') {
        // do delete
    }
});
David Fox
  • 10,603
  • 9
  • 50
  • 80
0

My Intro to ASP.NET MVC 3 explains this. Make sure you don't delete on a GET.

RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
-1

In regards to Stephan Walter's site...

MVC application

What about using using the [ChildActionOnly] attribute with some authentication in the ActionResult to protect against accidental deletes? What if you are using jQuery GET callbacks with an ActionResult that renders a PartialViewas he suggests that REST purists would defend the idea that GET requests should not change the state of your application? like:

//delete comment and make ajax callback
function del(aid, cid) {
    $.ajax("/Article/DelCom?AId=" + aid + "&" + "CId=" + cid, function (result) {
        $('#comments-partial').html(result);
    });

Or is this only applicable for links?

yardpenalty.com
  • 1,244
  • 2
  • 17
  • 32
  • Do not delete on a GET. Do you really want your antivirus software's link scanner or web browsers pre-fetch function (etc) to wipe your data as you browse around? – NickG Mar 13 '14 at 15:41
  • I did read this article and there was actually some reason why I suggested this technique I just can't remember why ATM and judging by the time of comment it was probably a long nite. It might have been that I wanted to use ajax and I said get??? Anyways, I do understand that typically the ajax .get callback should only update the content (shouldn't affect the state of the application) upon deletion and if I remember why I went this route I will respond. – yardpenalty.com Mar 14 '14 at 00:40
  • I remember now. What I did was used a get to create an empty object inserted into a form within a partial view so no INSERT was executed. Then I used '@Ajex.BeginForm' to actually persist the data. So this method is fine just not with DELETE so my apologies. – yardpenalty.com Mar 24 '14 at 08:09