1

I am trying to add a css class to a @html.actionlink and do not want to use the text that does in the link. I want to use a graphic instead.

Here is my code:

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

When I delete the"Edit" I get an error. Is it possible to use this statement and have an icon/image for the link? Thanks for answers to this newbie question. Andy

1 Answers1

0

i think that a more nicer approach for this would be to create an extension method for it with these overloads in your helper folder and then use it in your views. just depends upon the personal preference

  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));
}

using it

 @Html.ImageActionLink("../../Content/images/edit.png", "Edit", "Edit", new { id = item.UserId})
maztt
  • 12,278
  • 21
  • 78
  • 153