Ajax.ActionLink未调用控制器操作

本文关键字:控制器 操作 调用 ActionLink Ajax | 更新日期: 2023-09-27 18:25:23

我正在显示一个文件列表,并允许用户从列表中删除。delete按钮对控制器进行ajax调用,以运行"delete"操作。但是,永远不会调用删除操作。我正在获取AjaxOptions中定义的确认警报。值得一提的是,我使用WebForms引擎完成了这项工作,并将其移至Razor。此外,这是我第一次使用区域。如果我直接调用Delete操作,它会起作用。这是路由问题吗?

这是背后的代码

  public EmptyResult Delete(string fileName)
    {
        if (fileName.IsNullOrEmpty()) return null;
        var model = new Models.BenefitBookletModel();
        model.DeleteBooklet(fileName);
        return null;
    }

这是标记

    @Ajax.ActionLink("test", "Delete", new { fileName = item.FileName }, new AjaxOptions
                                                                                 {
Confirm = "Are you sure you want to delete " + item.FileName + "?",
OnComplete = "function(){deleteComplete('" + item.jsReferenceableFileName + "')}",
HttpMethod = "DELETE",
OnFailure = "function(){alert('Could not delete file');}"
              }, new { @class = "DeleteButton" }
                                                                             )

这是我的RegisterRoutes

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute("SubscriberAll","subscriber/{id}",new { controller = "Subscriber", action = "ShowAll" },new { id = @"'d+" } );
        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

区域注册的路线

context.MapRoute("Marketing_default","Marketing/{controller}/{action}/{id}",new { action = "Index", id = UrlParameter.Optional }

这是标记生成的

<a href="/Marketing/BenefitBooklet/Delete?fileName=MyFileName.pdf" data-ajax-method="GET" data-ajax-failure="function(){alert('Could not delete file');}" data-ajax-confirm="Are you sure you want to delete MyFileName.pdf?" data-ajax-complete="function(){deleteComplete('MyFileName.pdf')}" data-ajax="true" class="DeleteButton"> </a>

Ajax.ActionLink未调用控制器操作

您应该将函数名称指定为相应AjaxOptions属性的值。添加script部分:

<script type="text/javascript">
    function OnFailure(request, error) {
        alert('Could not delete file');
    }
    function OnComplete(request, error) {
        alert('Delete complete');
    }
</script>

在您的视图中,并在"AjaxOptions"中更改OnFailureOnComplete

OnFailure = "OnFailure"
OnComplete= "OnComplete"

您将Action Link的HttpMethod定义为DELETE,但您的方法可能只接受GET。试着用Delete动作动词来修饰它。

[HttpDelete]
public EmptyResult Delete(string fileName)