自定義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>
重複的:http://stackoverflow.com/questions/1974980/asp-net-mvc-putting-html-inside -html-ActionLink的加無鏈接文本 – 2012-06-15 10:34:37