MVC3 - Razor:如何在ActionLink名称中使用html标记

时间:2012-06-15 10:29:28

标签: html asp.net-mvc-3 razor

我的MVC3剃刀视图上有一个动作链接。其中有一个动作链接:

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

我想将操作链接文字更改为:

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

有没有办法解决它。

非常感谢

2 个答案:

答案 0 :(得分:4)

使用URL.Action代替操作链接,您可以更好地控制内容。

<a href="@Url.Action("Index", "Home")" class="button lines-6">
    <strong>Click Here </strong> (I do not have Middle Name)
</a>

答案 1 :(得分:1)

自定义HtmlHelper扩展是另一种选择。

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

您也可以尝试以上建议选项的其他风格:

<li id="home_nav"><a href="<%= Url.Action("ActionName") %>"><span>Span text</span></a></li>
相关问题