1

I have a action link on my MVC3 razor view. Which have a action link as bellow:

@Html.ActionLink("Click Here (I do not have Middle Name)", 
                 "", "", new { @class = "button lines-6" })

I want to change action link text to:

<strong>Click Here </strong> (I do not have Middle Name)

Is there any way to sort it out.

Many Thanks

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
user1211185
  • 731
  • 3
  • 12
  • 27
  • Duplicated: http://stackoverflow.com/questions/1974980/asp-net-mvc-putting-html-inside-html-actionlink-plus-no-link-text –  Jun 15 '12 at 10:34

2 Answers2

6

Using the URL.Action instead of an action link you have more control over the contents.

<a href="@Url.Action("Index", "Home")" class="button lines-6">
    <strong>Click Here </strong> (I do not have Middle Name)
</a>
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
1

A custom HtmlHelper extension is another option.

public static string ActionLinkSpan( this HtmlHelper helper, string linkText, string actionName, string controllerName, object htmlAttributes )
{
    TagBuilder spanBuilder = new TagBuilder( "span" );
    spanBuilder.InnerHtml = linkText;

    return BuildNestedAnchor( spanBuilder.ToString(), string.Format( "/{0}/{1}", controllerName, actionName ), htmlAttributes );
}

private static string BuildNestedAnchor( string innerHtml, string url, object htmlAttributes )
{
    TagBuilder anchorBuilder = new TagBuilder( "a" );
    anchorBuilder.Attributes.Add( "href", url );
    anchorBuilder.MergeAttributes( new ParameterDictionary( htmlAttributes ) );
    anchorBuilder.InnerHtml = innerHtml;

    return anchorBuilder.ToString();
}

You may also try a different flavor of above suggested option:

<li id="home_nav"><a href="<%= Url.Action("ActionName") %>"><span>Span text</span></a></li>
Yusubov
  • 5,815
  • 9
  • 32
  • 69