tags:

views:

505

answers:

1

Given an ASP.NET MVC view that generates a table of entries using a "for" loop, what is the best way to add a "delete" link for each of the table rows? My first intuition would be to use jQuery to make an AJAX call to delete the row, then refresh the table. It seems like there should be an easier way though. Maybe make the link perform a post to a delete url (e.g. /Item/Delete/1) which would redirect back to the page displaying the items?

+4  A: 

both are acceptable ways, and actually exactly the same.

In the first you are using AJAX to post to the URL and AJAX to refresh the table. In the second, you are not using AJAX.

Either way, posting the id to be deleted to the delete ActionMethod (I'd use Destroy, but that's personal preference) is the way to go.

To make it even better do something like this ...

[AcceptVerbs(HttpVerbs.Delete)]
    public ActionResult Detail (int id)
    {
        // Add action logic here

    }
Kyle West
Going forward from Kyle's post, some webservers deny DELETE requests, and therefore, you may need to fallback to POST'ing the ID. I don't think it will require too much of a change however (perhaps, just adding the Post HttpVerb).
Dan Atkinson