0

In my MVC application I have a grid that uses a HTML helper function. The EditorTemplate looks like;

<tr>
    <td>
        <%-- Ajax Delete --%>
        <% if(Model.LeaveId > 0) { %>
        <%: Html.DeleteEmployeeOtherLeave("Delete", Model)%>
        <%} %>
    </td>
    <td><%: Model.LeaveType %></td>
    <td><%: Model.MorningOnlyFlag %></td>
    <td><%: Model.AfternoonOnlyFlag %></td>
    <td><%: Model.DayAmount %></td>
    <td><%: String.Format("{0:ddd MMM d yyyy}", Model.Date)%></td>
</tr>

And the HTML helper looks like;

        public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave _leave)
        {
            return html.RouteLink(linkText, "Default",
                new { _employeeOtherLeaveId = _leave.LeaveId, action = "Delete" },
                new { onclick = "$.ajax({url: this.href, type: 'DELETE', success: function(result) {$('#wholepage').html(result);}}); return false;" }
        }

My controller looks like;

    [HttpGet]        
    [Authorize(Roles = "Administrator, AdminAccounts, ManagerAccounts")]
    public ActionResult EmployeeAbsence()
    {
        if ((SessionObjects.AbsenceStartDate > DateTime.MinValue) && (SessionObjects.AbsenceEndDate > DateTime.MinValue))
            if (SessionObjects.AbsenceSelectedEmployeeId == 0)
                return View(new AbsenceViewModel()
                {
                    AbsenceStartDate = SessionObjects.AbsenceStartDate,
                    AbsenceEndDate = SessionObjects.AbsenceEndDate
                });
            else
                return View(new AbsenceViewModel(
                    SessionObjects.AbsenceStartDate,
                    SessionObjects.AbsenceEndDate,
                    SessionObjects.AbsenceSelectedEmployeeId
                    ));

        return View();
    }

    [AcceptVerbs(HttpVerbs.Delete)]
    public ActionResult Delete(int _employeeOtherLeaveId)
    {
        EmployeeOtherLeaf.Delete(_employeeOtherLeaveId);
        return RedirectToAction("EmployeeAbsence");
    }
    #endregion

Using the Delete verb works in FF, but not in IE. Replacing the DELETE verb with POST seems to prevent it from working at all. How do I get this to work in IE?

arame3333
  • 9,887
  • 26
  • 122
  • 205

3 Answers3

1

Check this out.

http://stephenwalther.com/blog/archive/2009/01/21/asp.net-mvc-tip-46-ndash-donrsquot-use-delete-links-because.aspx

HTML Supports Only GET and POST

So, the proper thing to do when deleting a database record is to perform an HTTP DELETE operation. Performing an HTTP DELETE does not open a security hole and it does not violate REST principles.

Unfortunately, standard HTML does not support HTTP operations other than GET and POST. A link always performs a GET and a form can perform either a GET or POST. HTML does not support other types of HTTP operations.

According to the HTML 3.1 specification, the HTML FORM tag only supports GET and POST. It does not support other HTTP operations such as DELETE or PUT. See http://www.w3.org/TR/REC-html32.html#form. Furthermore, Internet Explorer only supports GET and POST (see http://msdn.microsoft.com/en-us/library/ms534167(VS.85).aspx).

goenning
  • 6,514
  • 1
  • 35
  • 42
  • When I change the DELETE verb to a POST, I find that I call the method and delete the record OK, but the RedirectToAction command does not seem to work. It does in FF but not in IE. I get System.Web.HttpException: A public action method 'EmployeeAbsence' was not found on controller 'SHP.Controllers.EmployeeController'. – arame3333 Mar 01 '11 at 15:39
1

Two things:

  1. Don't use HTTP 302 (redirect) with XHR, because...
  2. Changing DELETE to POST won't work, because your action method only executes when you do a DELETE:

    [AcceptVerbs(HttpVerbs.Delete)]
    

    If you'd change it to accept POST then it would work.

  3. If you're using MVC2+ you can also use HTTP method overriding.
Community
  • 1
  • 1
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • When I change the DELETE verb to a POST, I find that I call the method and delete the record OK, but the RedirectToAction command does not seem to work. It does in FF but not in IE. I get System.Web.HttpException: A public action method 'EmployeeAbsence' was not found on controller 'SHP.Controllers.EmployeeController'. – arame3333 Mar 01 '11 at 15:41
  • @arame3333: Have you tried removing (for test purposes) `Authorize` attribute on the `EmployeeAbsence` action? – Robert Koritnik Mar 02 '11 at 15:04
0

IE might not support DELETE requests. See Problem with jQuery.ajax with 'delete' method in ie

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636