I'm trying to use Html.AnchorLink to display an anchor link with an image inside and no text. I'm trying to pass the image tag in the first parameter of AnchorLink, the text parameter. My parameter begins "<img" and it comes out in the browser as "<img".
How can I do this? Is there an easier/better way to do this?
You can't pass html to the ActionLink Helper. It automatically html encodes the text (which is why your html comes out looking strange), that's actually a great security feature and protects against XSS attackys.
If you want to customize a link and add an image you have to construct the link using url.Action like so
I was able to get it to work with URL.Action. However, when the new window opens up it is a new window tab. What I really want it something like a modal popup window. I know how to do that with some javascript client-side. Is there an MVC way to do that
or should I just mix in the javascript client code?
MVC doesn't have a built in way to do that, but since it gives you full control over the html it's really easy to use javascript client code to do this. I would go that route.
DallasSteve
Member
222 Points
338 Posts
How to return HTML in AnchorLink
Apr 20, 2012 08:41 PM|LINK
I'm trying to use Html.AnchorLink to display an anchor link with an image inside and no text. I'm trying to pass the image tag in the first parameter of AnchorLink, the text parameter. My parameter begins "<img" and it comes out in the browser as "<img". How can I do this? Is there an easier/better way to do this?
www.SteveGaines.us
CodeHobo
All-Star
18669 Points
2648 Posts
Re: How to return HTML in AnchorLink
Apr 20, 2012 09:00 PM|LINK
I think you mean Html.ActionLink.
You can't pass html to the ActionLink Helper. It automatically html encodes the text (which is why your html comes out looking strange), that's actually a great security feature and protects against XSS attackys.
If you want to customize a link and add an image you have to construct the link using url.Action like so
<a href="@Url.Action("action","controller")"> <img src="/content/image.png" /> </a>Edit: Url.Action takes the same input parameters you would pass to Html.ActionLink minus the actual text of the link.
Blog | Twitter : @Hattan
ignatandrei
All-Star
137696 Points
22154 Posts
Moderator
MVP
Re: How to return HTML in AnchorLink
Apr 21, 2012 03:20 AM|LINK
Do you have a custom extension?
DallasSteve
Member
222 Points
338 Posts
Re: How to return HTML in AnchorLink
Apr 21, 2012 03:57 AM|LINK
CodeHobo
I was able to get it to work with URL.Action. However, when the new window opens up it is a new window tab. What I really want it something like a modal popup window. I know how to do that with some javascript client-side. Is there an MVC way to do that or should I just mix in the javascript client code?
Thanks
www.SteveGaines.us
CodeHobo
All-Star
18669 Points
2648 Posts
Re: How to return HTML in AnchorLink
Apr 21, 2012 07:01 AM|LINK
MVC doesn't have a built in way to do that, but since it gives you full control over the html it's really easy to use javascript client code to do this. I would go that route.
Blog | Twitter : @Hattan