DotnetSparrow
DotnetSparrow

Reputation: 27996

submitting form using hyperlink instead of button

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.

Upvotes: 2

Views: 2928

Answers (2)

Jen Grant
Jen Grant

Reputation: 2074

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;"> 

Upvotes: 4

Hector Correa
Hector Correa

Reputation: 26690

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

Upvotes: 2

Related Questions