【问题标题】:Call an Action using Ajax.ActionLink does not work使用 Ajax.ActionLink 调用 Action 不起作用
【发布时间】:2016-05-03 08:47:25
【问题描述】:

我有一个非常奇怪的问题,我写:

@Ajax.RawActionLink(
    "<i class=\"fa fa-print\"></i>",
    "CustomerOrder",
    "PrintSheet",
    new AjaxOptions()
    {
        UpdateTargetId = "bodyContent",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "GET"
    },
    new
    {
        @class = "btn btn-success",
        data_toggle = "tooltip",
        data_placement = "top",
        title = "Imprimer"
    })

但我明白了:

<a class="btn btn-success"
  data-ajax="true"
  data-ajax-method="GET"
  data-ajax-mode="replace"
  data-ajax-update="#bodyContent"
  data-placement="top"
  data-toggle="tooltip"
  href="/Sales/CustomerOrder?Length=10"
  title=""
  data-original-title="Imprimer">
    <i class="fa fa-print"></i>
</a>

在呈现的 Html 中。

我正在从另一个控制器调用 print CustomerOrder 操作,但我总是得到路径中的当前控制器,知道吗?

Ps:我正在使用 Ajax ActionLink 的扩展

        public static MvcHtmlString RawActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, AjaxOptions ajaxOptions, object htmlAttributes)
    {
        var repID = Guid.NewGuid().ToString();
        var lnk = ajaxHelper.ActionLink(repID, actionName, controllerName, ajaxOptions, htmlAttributes);
        return MvcHtmlString.Create(lnk.ToString().Replace(repID, linkText));
    }

【问题讨论】:

  • this 是从哪里获得 RawActionLink 的?
  • 我从 msdn 得到的,是的,它是一样的
  • 在 SO here 上有一个扩展。
  • 您应该在问题中包含扩展定义,以便每个人都清楚您是如何实现RawActionLink
  • 我认为是路由问题,错误出现在我们调用ajaxHelper.ActionLink的那一行

标签: asp.net-mvc razor-3


【解决方案1】:

假设 RawActionLink 包裹在 ActionLink 周围,似乎您的目标是错误的重载方法。试试:

@Ajax.RawActionLink(
    "<i class=\"fa fa-print\"></i>", //content
    "CustomerOrder", //action
    "PrintSheet", //controller
    new {}, //routing data <---- ADDED
    new AjaxOptions() //ajax options
    {
        UpdateTargetId = "bodyContent",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "GET"
    },
    new //html attributes
    {
        @class = "btn btn-success",
        data_toggle = "tooltip",
        data_placement = "top",
        title = "Imprimer"
    })

https://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxextensions.actionlink(v=vs.118).aspx#M:System.Web.Mvc.Ajax.AjaxExtensions.ActionLink%28System.Web.Mvc.AjaxHelper,System.String,System.String,System.String,System.Object,System.Web.Mvc.Ajax.AjaxOptions,System.Object%29

【讨论】:

    猜你喜欢
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    相关资源
    最近更新 更多