How to create your own MVC3 ActionLink method?

Possible duplicate:
How to set span element inside ActionLink MVC3?

How to create your own MVC3 ActionLink method that generates this output:

<li> <a href="/Home/ControllerName" data-ajax-update="#scroll" data-ajax-mode="replace" data-ajax-method="GET" data-ajax-loading="#progress" data-ajax="true"> <span>LinkText</span> // this span generated inside <a> </a> </li> 
+4
source share
2 answers

You either create a new extension method that returns the MvcHtmlString object that you put together (for example, html encoding), we create a partial view that you can display when you need it, t you need to create HTML through the code.

 public static class MyHtmlExtensions { public static MvcHtmlString MyActionLink(this HtmlHelper html, string action, string controller, string ajaxUpdateId, string spanText) { var url = UrlHelper.GenerateContentUrl("~/" + controller + "/" + action); var result = new StringBuilder(); result.Append("<a href=\""); result.Append(HttpUtility.HtmlAttributeEncode(url)); result.Append("\" data-ajax-update=\""); result.Append(HttpUtility.HtmlAttributeEncode("#" + ajaxUpdateId)); // ... and so on return new MvcHtmlString(result.ToString()); } } 
+7
source

You will need to create a special HTML helper for the razor. This way you can render your own HTML (including your span tag requirement) for your link. This auxiliary extent method should return an MvcHtmlString object.

One example of creating an HTML helper can be found here.

+2
source

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


All Articles