Ajax.ActionLink 不调用控制器操作

发布于 2025-01-08 12:45:56 字数 1964 浏览 2 评论 0原文

我正在显示文件列表并允许用户从列表中删除。删除按钮对控制器进行 ajax 调用以运行“删除”操作。但是,删除操作永远不会被调用。我收到 AjaxOptions 中定义的确认警报。无论如何,我使用 WebForms 引擎完成了这项工作,然后将其移至 Razor。另外,这是我第一次使用 Areas。如果我直接调用删除操作,它就会起作用。这是路由问题吗?

这是后面的代码

  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>

I'm displaying a list of files and allowing the user to delete from the list. The delete button does an ajax call to the controller to run the 'Delete' action. However, the delete action is never called.I am getting the confirmation alert defined in AjaxOptions. For what it's worth, I had this working using the WebForms engine and just moved it to Razor. Also, this is the first time I've used Areas. If I call the Delete action directly, it works. Is this a routing issue?

Here's the code behind

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

Here's the mark up

    @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" }
                                                                             )

Here is my 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 });

Routes from area registration

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

Here is the markup generated

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2025-01-15 12:45:56

您应该将函数名称指定为相应 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"

You should specify the function name as the value of the corresponding AjaxOptions property. Add script section:

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

in your view and change OnFailure and OnComplete in 'AjaxOptions':

OnFailure = "OnFailure"
OnComplete= "OnComplete"
山色无中 2025-01-15 12:45:56

您将操作链接的 HttpMethod 定义为 DELETE,但您的方法可能只接受 GET。尝试用删除动作动词来修饰它。

[HttpDelete]
public EmptyResult Delete(string fileName)

You are defining the HttpMethod of the Action Link to be DELETE but you method probably only accepts GET. Try decorating it with the Delete action verb.

[HttpDelete]
public EmptyResult Delete(string fileName)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文