Using image with HtmlHelpers or @Html.ActionLink

Sometimes we need to serve image with the link and with the release of Razor syntaxes we have HtmlHelpers like Html.ActionLink() that has nothing like image.

With MVC, the Html.ActionLink() creates a link to a controller and action, for example:

@Html.ActionLink("Text to display", "Index", "Home")

In above HtmlHelper, ‘Index’ is view name and ‘Home’ is controller name. If you use above HtmlHelper, you will see following output:


Now, how to use image??

There is some workaround that I frequently use:

With Action name and htmlAttribute:


With Action name, Controller name and htmlAttribute:


And now the class will be:


I hope this post helps you. Thanks.

Comments

  1. Great Post.
    Very Clever an Helpful.

    Note: If you want to add a description as a Tooltip (since the text is being "removed") just add @title="your_tool_tip". I.E.: .... new { @class="image", @title="your_tool_tip" }

    ReplyDelete

Post a Comment

Popular posts from this blog