1

I'm pretty new to MVC and Javascript; I'm trying to make a delete action work; I'm using an ActionLink with a Javascript function for confirmation. The JAvascript confirm doesn't work, the delete action is fired even when I press Cancel; additionally, I cannot seem to be able to use [HttpDelete] verb for the action: I need to pass two parameters to the Delete action but after applying @Html.HttpMethodOverride(Http.Delete) on the action link it searches for an URL with just one parmaeter: id.

Here is my code: action link

 @Html.HttpMethodOverride(HttpVerbs.Delete)
  @Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="confirmDeleteAction()"})

function confirmDeleteAction() {       
    return confirm('Are you sure you want to delete the notification ?');        
}

Delete action:

   [HttpDelete]
    public ActionResult Delete(int id, int typeId)
    {
        Service.DeleteNotification(id, typeId);

        NewIndexViewModel model = Service.GetNewIndexViewModel();
        return View("Index", model);
    }
2
  • for passing two parameters to Delete Method you have to add an entry in Route.config file Commented Jul 26, 2013 at 7:36
  • Visit blog If you want to use verbs Commented Jul 26, 2013 at 12:30

3 Answers 3

1

Try this

 @Html.ActionLink(
            "Delete", 
            "Delete", 
            new {id=notification.Id, typeId=notification.TypeId}, 
            new {onclick="return confirmDeleteAction();"})
Sign up to request clarification or add additional context in comments.

3 Comments

That worked, thanks ! Only one problem left: making the action work when using HttpDelete verb; Any idea why I can't pass two parameters to an action merked with the HttpDelete verb ?
@OctavianEpure Try to remove verb then it will work in simple MVC
I know it works without the verb, I just wanted to use the HttpDelete verb to avoid using GET. I found an article here which explained the security reasons for this. Also I would like to avoid POST, because I already have a submit button on the form. This being a complex index view from which multiple actions can be fired: add, edit, delete, archive items, I didn't want to over-complicate it with multiple post actions and also use a safe way to make a delete. Otherwise, I can use a GET delete action and hope for the best :)
0

You need to cancel the default behaviour of the browser when the user clicks cancel. You can do this by returning the result of confirmDeleteAction() to your <a> tag:

@Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="return confirmDeleteAction()"})

For clarity, I added the return keyword to your onclick handler which will return the result of confirmDeleteAction() to the browser -

true = perform the default behaviour of the link

false = do nothing

Comments

0

You can do this type by also.

@Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="confirmDeleteAction(" + notification.Id + ")" })


function OnDeleteClick(id) {
                    var ActionID = id;
                    var flag = confirm('Are you sure you want to delete this record?');
                    if (flag) {
                return true;
                }
        else{
        return false;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.