how to make html.actionlink without text but with image inside
Asked Answered
C

4

8

it is simple html:

<%--
<a href="?language=de">
    <img src="/Images/Company/de.png" alt="Webseite auf Deutsch" style="border: 0;" />
</a>
--%>

i would like to make from them html.actionlink:

 <%= Html.ActionLink("", "ChangeCulture", "Account", new { lang = "de", returnUrl = this.Request.RawUrl }, new { @style = "background-image: url(/Images/Company/de.png)", @class = "languageLink" }) %>

I would like to have my link without text. it doesn't work, null in a first parameter its not allowed. image is to short as normal. how can i use html.actionlink without text but with image??

Closelipped answered 14/9, 2010 at 7:5 Comment(1)
possible duplicate of Image equivalent of ActionLink in ASP.NET MVCGerda
M
11

Here is a solution.

<a href="<%= Url.RouteUrl("MyRoute", new { id = Model.Id }) %>">
   <img src="myimage.png" alt="My Image" />.
</a>

Essentially, ActionLink is for text links and there isn't an equivalent for images, but you can use Url.RouteUrl to get the address for the link and put any HTML code you like inside of the anchor (W3C permitting of course).

Miniature answered 14/9, 2010 at 7:16 Comment(0)
M
8

This way is easiest (using Razor):

<a href="@Url.Action("Action", "Controller", new { lang = "de", returnUrl = this.Request.RawUrl }, new { @style = "background-image: url(/Images/Company/de.png)", @class = "languageLink" })"><img src="myimage.png" alt="My Image" /></a>
Microfilm answered 27/10, 2011 at 8:15 Comment(1)
There's no htmlAttribute argument in the method @Url.Action. It should be @Url.Action(string actionName, string controllerName, RouteValueDictionary routeValues)Sylas
P
3

based on a previous SO question (that was closed), here's a solution:

public static string ImageLink(this HtmlHelper htmlHelper, 
    string imgSrc, string alt, 
    string actionName, string controllerName, object routeValues, 
    object htmlAttributes, object imgHtmlAttributes)
{
    UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
    string imgtag = htmlHelper.Image(imgSrc, alt, imgHtmlAttributes);
    string url = urlHelper.Action(actionName, controllerName, routeValues);

    TagBuilder imglink = new TagBuilder("a");
    imglink.MergeAttribute("href", url);
    imglink.InnerHtml = imgtag;
    imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

    return imglink.ToString();
}

the orignal can be found here: Is there an ASP.NET MVC HtmlHelper for image links?

Prescript answered 14/9, 2010 at 8:18 Comment(0)
B
1

Here is my humble contribution, using Razor (as an example) for when a Font Awsome character is used as an image:

<p>
@Html.ActionLink(" ", "Create", "RouteName", new { id = Model.ID }, new { @class = "fas fa-plus text-center" }) 
</p>

The character in between the two quotes is alt+255 (hit the ALT key, then key-in 255 on the num keyboard, then release the ALT key). Indeed, neither "" nor " " nor string.Empty work with ActionLink, but alt-255 does. This is a very old trick (from the DOS days) to display a hidden blank character. So far I have not encountered any problems with this trick.

Bandaid answered 17/7, 2020 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.