Create a delete link to call Delete action (POST method)

Topics: Writing modules
May 5, 2012 at 10:12 AM

Hi,

I want to create a delete link to call delete action (Post method).

It's similar delete link in the manager content section.

I have used bellow code to display a <a> element.

 

<a href="@Url.Action("Delete", "Customer",new {
                            area = "OTW",
                            externalIdentifier = Model.Accounts[accountIndex].Account.ExternalIdentifier,
                            returnUrl = Context.Request.RawUrl,
                            hashedProvider = Model.Accounts[accountIndex].Account.HashedProvider
                        })" itemprop="RemoveUrl UnsafeUrl">@T("Remove")</a>

But when I clicked it, the system don't call correct action.

Note: I don't use my view in admin shape.

Please give me some advices,

Thanks,

Hien Phan

 

Developer
May 5, 2012 at 10:23 AM

If your action is decorated with the [HttpPostAttribute], you need to wrap a <form> around your delete link. You then need to handle the click event of that link and submit the containing form. What I usually do is create a generic piece of javascript that adds an eventhandler to all links that have a "submit" class. That script would prevent the default action (which is to navigate to the specified url) and instead submit any form it finds as its parent.

May 5, 2012 at 12:16 PM
sfmskywalker wrote:

If your action is decorated with the [HttpPostAttribute], you need to wrap a <form> around your delete link. You then need to handle the click event of that link and submit the containing form. What I usually do is create a generic piece of javascript that adds an eventhandler to all links that have a "submit" class. That script would prevent the default action (which is to navigate to the specified url) and instead submit any form it finds as its parent.

Thank you very much!