C# 如果链接文本包含 html,则如何在 ASP .NET MVC Razor 视图引擎中使用 @Html.ActionLink() 生成链接,也应该呈现该链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12803588/
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
How to generate link using @Html.ActionLink() in ASP .NET MVC Razor view engine if link text contains html, which should be rendered too?
提问by Dmytro
Possible Duplicate:
Razor syntax prevent escaping html in an action link
可能的重复:
Razor 语法防止在操作链接中转义 html
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:
如何使用 ASP.NET MVC Razor 视图引擎@Html.ActionLink()方法生成包含图像的链接。此方法将内部链接文本作为第一个参数,所以我尝试了:
@{
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" /> Tilesinner text. HTML didn't render.
但是,结果,我得到了与<img src="/source.img" /> Tiles内部文本的链接。HTML 没有呈现。
采纳答案by spender
Write it out longhand, using Url.Actionto render the hrefattribute value.
直接写出来,Url.Action用来渲染href属性值。
<a href="@Url.Action("action"...)">
<img src="@MyClass.GetImgSrc()" /> Tiles
</a>
回答by Patrick Dubois
ASP.NET have already a utility to create html from string with System.Web.MVC
ASP.NET 已经有一个实用程序可以使用 System.Web.MVC 从字符串创建 html
@MvcHtmlString.Create(linkInnerHtml)
In you case
在你的情况下
@Html.ActionLink(MvcHtmlString.Create(linkInnerHtml), "action" ... )

