2012-02-08 143 views
4

是否有任何地方可以看到當前Ajax.ActionLink方法的方法定義,因爲我希望能夠使用自定義方法擴展它,但不確定其當前方法定義ASP.NET MVC - 擴展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()); 
     } 
    } 

但顯然我需要添加AjaxOptions東西,它的默認Ajax.ActionLink包含,但我不知道什麼是定義好像。

之所以這樣做,這是我想要的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 

回答