how to insert image in html action link? asp.net mvc
Asked Answered
S

5

8

I have navigation and many link on my webproject from html action links. They are ugly with underline. I would like to insert some image with name or play with styles of action link. Is it possible? How to do that?

Thanks and take care, Ragims

Saylor answered 2/9, 2010 at 12:43 Comment(0)
C
17

You could use css to remove the underlines or place a backgroundimage, otherwise you could also just create the link like so:

<a href="<%: Url.Action("Action", "Controller")%>"><img src="yourimg.jpg" /></a>
Counterword answered 2/9, 2010 at 12:45 Comment(3)
iam using html.actionlink, and not url.actionSaylor
Yeah, i know, but with Html.ActionLink you can't add any other content to the link than plain text.Counterword
If the Action would link to the homepage and the image would be a logo, will it cause an infinite loop? I keep getting stack overflow when I do that. If I remove it, my page works well. I thought for the action to run I have to click on it?Cantrell
H
2

Html.ActionLink and Url.Action return the same URL. The difference is that the first creates an HTML element while the second returns just the URL to that action.

Another option is to use Url.RouteUrl or Html.RouteLink to create a link based off your route (to an action) instead of directly to an action.

Huarache answered 2/9, 2010 at 13:3 Comment(0)
T
0

One solution is to create an HtmlHelper extension method for creating an image-specific action link. A detailed tutorial can be found here.

Taction answered 2/9, 2010 at 13:21 Comment(0)
A
0

If You are on MVC 3-4 with razor view engine then this may help you-

@Html.ActionLink("your link Name", "Action Method", "Controller", 
        new { style = "background-image:url('.././Images/imageyouwanttoshow.png')" },null)
Arterio answered 13/3, 2014 at 9:42 Comment(0)
S
0

Instead of using @Html.ActionLink("linkname","action","controller") you can use following

<a href='@Url.Action("action", "controller")'>
<img src='@Url.Content("~/images/imageName.png")' />

"images" is my folder for storing the images. @Url.Content() is to know the path. You can pass your action and the controller for that action to @Url.Action(). @Url.Action() works similar to the @Html.ActionLink(). Now your link will get replaced by the image.

Swear answered 29/9, 2015 at 6:49 Comment(1)
<a href='@Url.Action("action", "controller")'> <img src='@Url.Content("~/images/imageName.png")' /> </a>Swear

© 2022 - 2024 — McMap. All rights reserved.