Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

asp.net mvc - Using HTML tags inside linkText of Html.ActionLink

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...