You do not need to return a string. Take a MvcHtmlString
. Create an extension method like this:
public static MvcHtmlString CustomActionLink( this HtmlHelper htmlHelper, SqlConnection con)
{
//do your retrival logic here
// create a linktext string which displays the inner text of the anchor
// create an actionname string which calls the controller
StringBuilder result = new StringBuilder();
result.Append(linktext);
result.Append(actionname);
return new MvcHtmlString(result);
}
In your view:
@Html.CustomActionLink(SqlConnection con)
You need to import the namespace System.Web.Mvc.Html
AND make sure your route is defined in the RouteConfig.cs
or whereever you define your custom routes.
An Important note: Your final string (result), which is returned needs to be in the format:
<a href='/Controller/Action/optionalrouteparameters'>LinkText</a>
The MvcHtmlString() makes sure every possible character like =, ? & \ are properly escaped and the link is rendered correctly
For reference see msdn: http://msdn.microsoft.com/en-gb/library/dd493018(v=vs.108).aspx