Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate link using @Html.ActionLink() in ASP .NET MVC Razor view engine if link text contains html, which should be rendered too? [duplicate]

Possible Duplicate:
Razor syntax prevent escaping html in an action link

How can I generate link, which contains image, using ASP.NET MVC Razor view engine @Html.ActionLink() method. This method takes inner link text as first parameter, so I tried:

@{
string linkInnerHtml = string.Format("<img src=\"{0}\" /> Tiles", MyClass.GetImgSrc());
}
@Html.ActionLink(linkInnerHtml, "action" ... )

but, as a result, I got link with <img src="/source.img" /> Tiles inner text. HTML didn't render.

like image 754
Dmytro Avatar asked Oct 09 '12 15:10

Dmytro


People also ask

What is HTML ActionLink ()?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

What is the difference between using URL action and HTML ActionLink in a view?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

Which ASP.NET MVC object generates the HTML output that displays in a web browser?

Razor is a powerful templating implementation that's very useful to generate text and HTML output for all sorts of purposes. As you've seen in this article, you can use ASP.NET MVC Razor views from just about anywhere in your ASP.NET applications fairly easily with the Helper class provided here.

How do you pass an ActionLink model?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to send Model data (object). Hence in order to pass (send) Model data (object) from View to Controller using @Html.


1 Answers

Write it out longhand, using Url.Action to render the href attribute value.

<a href="@Url.Action("action"...)">
  <img src="@MyClass.GetImgSrc()" /> Tiles
</a>
like image 169
spender Avatar answered Oct 12 '22 19:10

spender