2012-06-03 19 views
1

我試圖將一個css類添加到@ html.actionlink並且不想使用鏈接中的文本。我想用圖形代替。如何使用@ html.actionlink刪除鏈接文本

這裏是我的代碼:

@Html.ActionLink("Edit", "PopupReferenceEdit", new { id = item.VolunteerReferenceID }, new { @class = "Grid-editor" }) 

當我刪除「編輯」我得到一個錯誤。是否有可能使用此聲明併爲鏈接提供圖標/圖像? 感謝您解答這個新手問題。 Andy

+1

最相關的問題,但這裏是一個使用CSS隱藏文字提問:HTTP:// stackoverflow.com/q/596444/342156 –

+0

你試過空串嗎? @ Html.ActionLink(「」,「PopupReferenceEdit」,新... – killebytes

回答

0

我認爲更好的方法是爲你的助手文件夾中的這些重載創建一個擴展方法,然後在你的視圖中使用它。只是取決於個人喜好

public static class ImageActionLinkHelper 
    { 
    public static string ImageActionLink(this HtmlHelper helper, string ImageUrl, string altText, string actionName, object routeValues) 
    { 

    var builder = new TagBuilder("img"); 
    builder.MergeAttribute("src", ImageUrl); 
    builder.MergeAttribute("alt", altText); 
    builder.MergeAttribute("title", altText); 
    var link = helper.ActionLink("[replaceme]", actionName, routeValues, new { @class = "imgicon" }); 
    return link.ToString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)); 
} 




public static string ImageActionLink(this HtmlHelper helper, string ImageUrl, string altText, string actionName, object routeValues, string Id, string display) 
{ 
    var builder = new TagBuilder("img"); 
    builder.MergeAttribute("src", ImageUrl); 
    builder.MergeAttribute("alt", altText); 
    builder.MergeAttribute("title", altText); 
    var link = helper.ActionLink("[replaceme]", actionName, routeValues, new { @class = "imgicon", id = Id, style = display }); 

    return link.ToString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)); 
} 

使用它SO建議手動創建``元素

@Html.ImageActionLink("../../Content/images/edit.png", "Edit", "Edit", new { id = item.UserId}) 
+0

太棒了!非常感謝。Andy –