2012-06-15 40 views
1

我在我的MVC3剃鬚刀視圖上有一個操作鏈接。其中有一個動作鏈接波紋管:MVC3 - Razor:如何在ActionLink中使用html標籤名稱

@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)

有沒有辦法把它整理出來。

非常感謝

+0

重複的:http://stackoverflow.com/questions/1974980/asp-net-mvc-putting-html-inside -html-ActionLink的加無鏈接文本 – 2012-06-15 10:34:37

回答

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

自定義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> 
相關問題