ASP.NET MVC - Ajax.ActionLink Method Extension

Somewhere I can see the method definition of the current Ajax.ActionLink method, since I would like to extend it with a custom method, but I'm not sure about the current method definition

EDIT: This is what I have so far:

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()); } } 

But obviously, I need to add AjaxOptions stuff that contains Ajax.ActionLink by default, but I'm not sure how it looks.

The reason for this is that I want the linkText to be an HtmlString since I want to do something like this: @Ajax.MyActionLink("<span class="highlight">Hello</span> World",...)

Second edit:

Now I get a compilation error:

 ...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 

Here is my extension method:

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

Here is how I use it:

 @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 }) 

Third Editing:

I changed the extension method:

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

and now I get the following error:

 Cannot evaluate expression because the current thread is in a Qaru state. System.Collections.IDictionary 
+4
source share

Source: https://habr.com/ru/post/1395388/


All Articles