【问题标题】:Ajax.ActionLink not calling controller actionAjax.ActionLink 不调用控制器动作
【发布时间】:2012-03-13 04:26:58
【问题描述】:

我正在显示文件列表并允许用户从列表中删除。删除按钮对控制器进行 ajax 调用以运行“删除”操作。但是,永远不会调用删除操作。我收到了 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" }
                                                                             )

这是我的注册路由

        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>

【问题讨论】:

  • 请从您注册路线的 Global.asax 中添加信息。
  • Delete 方法是否与渲染视图在同一个控制器中?如果没有,您将需要使用 ActionLink(AjaxHelper, String, String, String, Object, AjaxOptions) 重载来定义控制器。
  • 是的,两者都在同一个控制器中。
  • 您在问题中提到了“区域”,但我在代码/路线中没有看到任何表明您正在使用区域的内容。这个删除操作是在不同的区域吗?
  • 没有。所有有问题的东西都在同一个区域。不知道在使用区域时我是否缺少任何东西。当它在 webforms 视图引擎中工作时,我没有使用它们并且没有问题。认为值得一提。

标签: c# asp.net asp.net-mvc-3 razor routing


【解决方案1】:

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

【讨论】:

  • 这就是问题所在。 function() 引起了问题。谢谢!
【解决方案2】:

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

[HttpDelete]
public EmptyResult Delete(string fileName)

【讨论】:

  • 尝试了 HttpDelete 和 [AcceptVerbs(HttpVerbs.Delete)]。控制器动作不是从 Ajax.ActionLink 调用的
  • 它将无法正常工作,因为您无法链接到将执行 HTTP 删除的操作,这将只是一个 fake delete 请求,我希望您从中获得 404点击那个链接。
  • 页面有JS错误吗?你有所有的js文件吗?包括 jquery.unobtrusive-ajax.min.js。您可以尝试将其更改为[HttpPost]HttpMethod = "POST"
  • 这样做并检查了 Firebug。显示错误“函数语句需要名称 > function(){alert('Could not delete file');}.'来自 OnFailure AjaxOption... 奇怪。
  • 试试OnFailure = "(function(){alert('Could not delete file');}())" 或者把你的成功和失败函数放在一个js文件里,只需要OnFailure = functionName
猜你喜欢
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
相关资源
最近更新 更多