asp.net-mvc 在 Html.ActionLink 的 linkText 中使用 HTML 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4936681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:51:36  来源:igfitidea点击:

Using HTML tags inside linkText of Html.ActionLink

asp.net-mvcactionlinkhtml.actionlink

提问by Tim Banks

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 的 linkText 中使用 HTML 标签?例如,如果我想加粗链接文本的一部分,我会尝试类似的操作:

<%= 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.

我知道我可以通过使用锚标记并使用 Url.Action 设置 URL 来做到这一点,但我只是想知道这是否可行。

采纳答案by SLaks

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

不; 这是不可能的。
您需要手动编写<a>标签。

回答by Nevada Williford

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

Html.ActionLink 帮助程序 HTML 对链接文本进行编码,以防止您在链接文本中嵌入 HTML。

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

出于同样的原因,您不能使用 Html.ActionLink 并传入标签来使图像成为超链接。

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 重载之一通过匿名对象语法指定 CSS 样式,如下所示...

@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>

回答by zlspjp

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.

本质上,使用 ActionLink 创建带有要替换内容的占位符 ('XXX') 的 html,然后将其转换为 HTML 字符串,用您的标记替换占位符,将字符串呈现为 HTML.Raw。

回答by Martin Buberl

It's not possible, but you could create a HtmlHelperfor this, see hereor use Url.Actioninstead what I would recommend you.

这是不可能的,但您可以HtmlHelper为此创建一个,请参阅此处或使用Url.Action我建议您的内容。