MVC:无法重构我的HtmlHelper以使用客户端脚本函数

时间:2011-02-07 12:12:27

标签: jquery asp.net-mvc

我有自定义的HtmlHelper;

public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave _leave)
{
    string url = string.Format("/Payroll/Delete?_employeeOtherLeaveId={0}", _leave.LeaveId.ToString());
    return html.RouteLink(linkText, "Default",
        new { _employeeOtherLeaveId = _leave.LeaveId, action = "Delete" },
        //new { @class = "delete-link" }
        new { onclick = "$.ajax({url: this.href, type: 'DELETE', success: function(result) {$('#wholepage').html(result);}}); return false;" }
}

正如您所看到的,我的RouteL命令的onclick事件有一个长字符串。 我想把它放在一些不引人注目的Javascript中,然后输入一些额外的javascript来确认弹出窗口。 因此,如果我注释掉onclick事件并取消注释该类,我打算使用以下Javascript函数

$('#delete-link').click(function () {
    var flag = confirm('Are you sure you wish to delete this item?');
    if (flag == true) {
        $.ajax({
            url: this.href,
            type: 'DELETE',
            success: function (result) {
                // update some DOM element with the result returned by the
                // server. So supposing that you have some <div id="someContainer">
                // that will contain the part of the DOM you want updated:
                $('#wholepage').html(result);
            }
        });
    }
    return false;
});

问题在于得到错误; System.Web.HttpException:在控制器'SHP.Controllers.PayrollController'上找不到公共操作方法'Delete'

但这是一种公共方法,为什么我应该这样做呢?

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

有些人指出this.url会有错误的价值。这似乎是这种情况,Firebug报告this.href是未定义的。 所以我改变了如下的辅助方法;

  public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave _leave)
    {
        string url = string.Format("/Payroll/Delete?_employeeOtherLeaveId={0}", _leave.LeaveId.ToString());
        return html.RouteLink(linkText, "Default",
            new { _employeeOtherLeaveId = _leave.LeaveId, action = "Delete" },
            new { onclick = "DeleteRow('" + url + "')" }
     }

javascript函数看起来像这样;

function DeleteRow(url) {
    $.ajax({
        url: url,
        type: 'DELETE',
        success: function (result) {
            $('#wholepage').html(result);
        }
    });
    return false;
}

我仍然得到同样的错误。

1 个答案:

答案 0 :(得分:0)

您的问题是jQuery不支持通过Ajax进行Delete调用,因此它正在执行GET或POST,这在控制器上找不到,因为您限制删除。

请参阅此处了解如何解决此问题:

http://homework.nwsnet.de/news/9132_put-and-delete-with-jquery

希望这有帮助

增加:

我已经对此进行了更多研究,最新的jQuery可能会支持它,但声明:

  

type String默认值:'GET'的类型   请求(“POST”或“GET”),   默认为“GET”。注意:其他HTTP   请求方法,如PUT和   DELETE,也可以在这里使用,但是   它们并非所有人都支持   浏览器。

MVC在flakey浏览器支持中的方式是为删除名为_method的表单添加隐藏字段并为其赋值delete

所以你可能希望添加到你的ajax代码:

$('#delete-link').click(function () {
    var flag = confirm('Are you sure you wish to delete this item?');
    if (flag == true) {
        $.ajax({
            url: this.href,
            data: {_method: "delete"},
            type: 'POST',
            success: function (result) {
                // update some DOM element with the result returned by the
                // server. So supposing that you have some <div id="someContainer">
                // that will contain the part of the DOM you want updated:
                $('#wholepage').html(result);
            }
        });
    }
    return false;
});