【问题标题】:MVC3 - Razor: How to use html tag in ActionLink NameMVC3 - Razor:如何在 ActionLink 名称中使用 html 标记
【发布时间】:2012-06-15 10:29:28
【问题描述】:
我的 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)
有没有办法解决。
非常感谢
【问题讨论】:
标签:
html
asp.net-mvc-3
razor
【解决方案1】:
使用URL.Action 而不是操作链接,您可以更好地控制内容。
<a href="@Url.Action("Index", "Home")" class="button lines-6">
<strong>Click Here </strong> (I do not have Middle Name)
</a>
【解决方案2】:
自定义 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>