2

I have created a link for deletion in asp.net mvc3 razor view like this:

 <td>
                @Html.ActionLink("Edit", "EditCategory", new { id = item.CategoryId }) |
                @Html.ActionLink("Details", "Details", new { id = item.CategoryId }) |

                @using (Html.BeginForm("Delete", "Admin"))
                {
                    @Html.Hidden("id", item.CategoryId)
                    <input type="submit" value="Delete" />
                }
            </td>

and created an action method like this:

 [HttpPost]
        public ActionResult Delete(int Id)
        {

            Category category = repository.Categories().FirstOrDefault(c => c.CategoryId == Id);
            if (category != null)
            {
                repository.DeleteCategory(category);
                TempData["message"] = string.Format("{0} was deleted", category.CategoryName);
            }
            return RedirectToAction("Categories");
        }

It works fine. but I want to use hyperlink for delete as I am using for edit and details. How can I replace the button with actuionlink. I tried that but it is not going in delete post action link and I am getting view not found error.

2
  • 1
    See this post: stackoverflow.com/questions/2048778/… Commented Oct 24, 2011 at 13:56
  • Your delete action must bring you to a page where you implement AntiForgeryToken and a confirmation to delete. This is essential to prevent CSRF (cross site request forgery) type attacks. Commented Oct 24, 2011 at 14:17

2 Answers 2

4

You can make it look like a link instead of a button ussing css, something like this.

<input type="submit" value="delete" style="border: none; background: none; 
                                           text-decoration: underline; 
                                           cursor: pointer;"> 
Sign up to request clarification or add additional context in comments.

Comments

2

Be careful not to make your Delete actions available via an HTTP GET. The HTTP spec recommends that HTTP POST is used for destructive actions (like a delete.) This prevents web crawlers from executing them.

By default hyperlinks issue HTTP GET. If you change your Delete action to a hyperlink make sure you override its behavior (via JavaScript) to issue a HTTP POST. Then you need to consider what happens if the browser has JavaScript turned off.

Another approach that you might want to consider is to [[make your Delete button look like a hyperlink via CSS styles[1]].

[1] http://www.xlevel.org.uk/post/How-to-style-a-HTML-Form-button-as-a-Hyperlink-using-CSS.aspx

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.