如何使用@ html.actionlink删除链接文本

时间:2012-06-03 23:44:04

标签: asp.net-mvc razor

我正在尝试将一个css类添加到@html.actionlink,并且不想使用链接中的文本。我想改用图形。

这是我的代码:

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

当我删除“编辑”时,我收到错误消息。是否可以使用此声明并为链接添加图标/图像? 感谢这个新手问题的答案。 安迪

1 个答案:

答案 0 :(得分: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));
}

使用它

 @Html.ImageActionLink("../../Content/images/edit.png", "Edit", "Edit", new { id = item.UserId})
相关问题