Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom MVC3 ActionLink method? [duplicate]

Possible Duplicate:
How to put span element inside ActionLink MVC3?

How to create custom 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>
like image 944
Davor Zubak Avatar asked Jan 19 '23 01:01

Davor Zubak


1 Answers

You either create a new extension method that returns an MvcHtmlString object that you put together yourself (mind the html encoding, though), our you create a partial view that you can render when you need it, so you don't have to create HTML through 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());
    }
}
like image 98
Anders Marzi Tornblad Avatar answered Jan 28 '23 09:01

Anders Marzi Tornblad