ActionLink Uninstall Confirmation

I want a Javascript popup to ask if I want to delete the list.

Here is what I have at the moment (doesn't work - Guid is NULL when it is caught by the controller).

@Html.ActionLink("Delete", "Delete", "Listing", listing.Guid, new { onclick = "return confirm('Are you sure you want to delete this listing?')" }) 

The first Delete is the string, the second Delete is the name of the method for the method. Listing is the name of the controller, listing.Guid is the parameter I want to send, and finally on-line is Javascript.

Any idea that I can go horribly wrong?

Change Also, any idea how I can have a prettier confirmation dialog? Using Bootstrap.

+4
source share
1 answer

You can use an anonymous object to pass route values:

 @Html.ActionLink( "Delete", "Delete", "Listing", new { id = listing.Guid }, new { onclick = "return confirm('Are you sure you want to delete this listing?')" } ) 

and the corresponding controller action:

 public ActionResult Delete(Guid id) { ... } 

And yes, it’s a very bad idea to use ActionLinks and GET requests to invoke actions that change the state on the server (for example, delete something). A much better approach is to use the correct HTTP verb for this case - DELETE. But since HTML forms do not universally support this verb, you can either use AJAX or use the POST verb:

 @using (Html.BeginForm("Delete", "Listing", new { id = listing.Guid }, FormMethod.Post, new { onsubmit = "return confirm('Are you sure you want to delete this listing?');" })) { @Html.HttpMethodOverride(HttpVerbs.Delete) <button type="submit">Delete</button> } 

and the corresponding controller action:

 [HttpDelete] public ActionResult Delete(Guid id) { ... } 
+9
source

Source: https://habr.com/ru/post/1434831/


All Articles