ASP.NET MVC - 扩展Ajax.ActionLink方法

时间:2012-02-08 10:40:13

标签: asp.net-mvc-3

我是否可以在任何地方看到当前Ajax.ActionLink方法的方法定义,因为我希望能够使用自定义方法扩展它但不确定其当前的方法定义

编辑:这是我到目前为止所做的:

public static class AjaxExtensions
    {
        public static IHtmlString MyActionLink(
        this AjaxHelper htmlHelper,
        string linkText,
        string action,
        string controller,
        object routeValues,
        AjaxOptions ajaxoptions,
        object htmlAttributes
    )
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
            var anchor = new TagBuilder("a");
            anchor.InnerHtml = linkText;
            anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);
            anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(anchor.ToString());
        }
    }

但显然我需要添加默认Ajax.ActionLink包含的AjaxOptions内容,但我不确定它的定义是什么样的。

这样做的原因是我希望linkText成为一个HtmlString,因为我想这样做:@Ajax.MyActionLink("<span class="highlight">Hello</span> World",...)

第二次编辑:

我现在收到编译错误:

...does not contain a definition for 'MyActionLink' and the best extension method overload 'MyProject.Helpers.AjaxExtensions.MyActionLink(System.Web.Mvc.AjaxHelper, System.Web.HtmlString, string, object, System.Web.Mvc.Ajax.AjaxOptions)' has some invalid arguments

这是我的扩展方法:

public static IHtmlString MyActionLink(
        this AjaxHelper ajaxHelper,
        HtmlString linkText,
        string action,
        object routeValues,
        AjaxOptions ajaxoptions
    )
        {
            return MyActionLink(ajaxHelper, linkText, action, routeValues, ajaxoptions);
        }

以下是我如何使用它:

@Ajax.MyActionLink(@Html.Raw(item.ITEMID.Replace((string)ViewData["ID"], "<span class='highlighted'>" + (string)ViewData["ID"] + "</span>")), "MoreDetails", new { id = item.ITEMID }, new AjaxOptions()
                       {
                           UpdateTargetId = "MoreDetails-" + item.ITEMID,
                           InsertionMode = InsertionMode.Replace
                       })

第三次编辑:

我更改了我的扩展方法:

public static IHtmlString MyActionLink(
            this AjaxHelper ajaxHelper,
            IHtmlString linkText,
            string action,
            object routeValues,
            AjaxOptions ajaxoptions
        )
            {
                return MyActionLink(ajaxHelper, linkText, action, routeValues, ajaxoptions);
            }

现在我收到以下错误:

Cannot evaluate expression because the current thread is in a stack overflow state. System.Collections.IDictionary

2 个答案:

答案 0 :(得分:5)

答案 1 :(得分:0)