是否有任何地方可以看到当前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
虽然这可能在理论上回答这个问题,[这将是更可取的](http://meta.stackexchange.com/q/8259)包括的基本部分答案在这里,并提供参考链接。 – 2012-02-09 14:20:20