Using HTML tags inside linkText of Html.ActionLink
Asked Answered
B

4

20

Is it possible to use HTML tags in the linkText of Html.ActionLink? For instance, if I wanted to bold part of the text of a link I would try something similar to this:

<%= Html.ActionLink("Some <b>bold</b> text", "Index")%>

but that just outputs

Some <b>bold</b> text

I know I could do this by using an anchor tag and setting the URL with Url.Action, but I just wanted to know if this was possible.

Bookbindery answered 8/2, 2011 at 18:11 Comment(1)
Possible duplicate of Putting HTML inside Html.ActionLink(), plus No Link Text?Meticulous
T
10

No; it's not possible.
You need to manually write an <a> tag.

Toein answered 8/2, 2011 at 18:12 Comment(5)
Thanks. I just wanted to make sure before manually writing the a tagsBookbindery
After 3 years is it still impossible?Diabolo
After 4 years is it still impossible?Mccarley
After 5 years is it still impossible?Cirque
@MartinBuberl: Yes. These helpers are for simple use cases; they were never intended to replace HTML tags.Toein
T
33

The Html.ActionLink helper HTML encodes the link text which prevents you from embedding HTML in the link text.

For this same reason you cannot use Html.ActionLink and pass in an tag to make an image a hyperlink.

For basic styling of a link, I'd recommend using one of the Html.ActionLink overloads to specify a CSS style via the anonymous object syntax like so...

@Html.ActionLink("Please Edit Me", "Edit", null, new { style="font-weight:bold;" })

Unfortunately, that applies bold to the entire text of the hyperlink when what you're wanting is just the word Edit to be bold. In which case I would do this...

<a href="@Url.Action("Edit")">Please <b>Edit</b> Me</a>

... or this ...

<a href="@Url.Action("Edit")">Please <span style="font-weight:bold;">Edit</span> Me</a>
Tarrel answered 8/2, 2011 at 19:37 Comment(3)
If Edit is an action of a different controller, how do I reference it? For example, I have two controllers: Home and Account. If I'm currently viewing the Login page of the Account controller, and I type @Url.Action("Home") then it renders: Account/Home. I'd like it to render as Home/Index where Home is the controller and Index is the action. I tried doing /Home/Index.Cate
I found the answer to my question is to use the second parameter of the @Url.Action(string, string) method. The second parameter is the controller name you wish to target.Cate
The @Url.Action method is the most flexible option, since it allows you to manipulate the anchor tag furtherCoot
T
10

No; it's not possible.
You need to manually write an <a> tag.

Toein answered 8/2, 2011 at 18:12 Comment(5)
Thanks. I just wanted to make sure before manually writing the a tagsBookbindery
After 3 years is it still impossible?Diabolo
After 4 years is it still impossible?Mccarley
After 5 years is it still impossible?Cirque
@MartinBuberl: Yes. These helpers are for simple use cases; they were never intended to replace HTML tags.Toein
O
1

This works for me:

@Html.Raw(@Html.ActionLink("XXX", "Index", new { }, new { @class = "FormBtn" }).ToHtmlString().Replace("XXX","<u>Back to List</u>"))

Essentially use the ActionLink to create the html with a placeholder for what you want to replace ('XXX'), then convert it an HTML String, replace the placeholder with your markup, render the string as HTML.Raw.

Oversight answered 19/8, 2016 at 18:22 Comment(0)
C
0

It's not possible, but you could create a HtmlHelper for this, see here or use Url.Action instead what I would recommend you.

Cirque answered 8/2, 2011 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.