asp.net-mvc ASP.NET MVC:如何在来自 Html.ActionLink 的链接中包含 <span>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1547097/
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
ASP.NET MVC: how to include <span> in link from Html.ActionLink?
提问by royco
I need the text of a link wrapped with <span>as in:
我需要用以下内容包裹的链接文本<span>:
<a href="/foo.html"><span>Edit Group</span></a>
Can I do that with Html.ActionLink? This doesn't work:
我可以用 Html.ActionLink 做到这一点吗?这不起作用:
<%=Html.ActionLink("<span>Edit Group</span>", "Edit", New With {.id = "bar"})%>
It just HTML encodes the <and >as <and >.
它只是 HTML 将<and编码>为<and >。
Is there a simple solution, or should I build the links by hand with Url.Action?
是否有一个简单的解决方案,或者我应该使用 Url.Action 手动构建链接?
This is for use with jQuery-UI Tabs. Tab labels need to be wrapped in <span>to get animation when AJAX content is loading.
这是用于 jQuery-UI 选项卡。<span>加载 AJAX 内容时,需要将标签标签包裹起来以获得动画。
采纳答案by womp
You'll need to do it with Url.Action, there's no way with Html.ActionLinkas far as I know.
你需要用 来做Url.Action,Html.ActionLink据我所知没有办法。
回答by David Andres
You can use the Url.Action helper method as a workaround (in case no other answers better fit your needs).
您可以使用 Url.Action 辅助方法作为解决方法(以防没有其他答案更适合您的需求)。
For example, the following can be in marked up in your view:
例如,您可以在视图中标记以下内容:
<a href="<%= Url.Action("Edit",
New With {.id = "bar"}) %>">
<span>Edit Group</span>
</a>
回答by cory-fowler
You can also roll your own HtmlHelper Extension Method, I actually prefer this method as you can control the placement of ids, classes, and other attributes like a title.
你也可以滚动你自己的 HtmlHelper 扩展方法,我实际上更喜欢这种方法,因为你可以控制 id、类和其他属性(如标题)的位置。
Here is a blog postthat I put together on the subject.
回答by RayLoveless
Here's a simple helperExtension example that worked for me:
这是一个对我有用的简单 helperExtension 示例:
http://forums.asp.net/p/1702210/4518688.aspx/1?Re+Quick+question+about+Ajax+ActionLink+and+span
http://forums.asp.net/p/1702210/4518688.aspx/1?Re+Quick+question+about+Ajax+ActionLink+and+span
回答by user3405201
What about this:
那这个呢:
@{
var link = Html.ActionLink("{0}", "Edit", New {id = "bar"}).ToString();
var url = string.Format(link, "<span>Edit Group</span>");
}
@Html.Raw(url);
//NICE HACK: Recommend using previous advice and write a helper to wrap this up.
//NICE HACK:推荐使用以前的建议并编写一个帮助程序来结束它。

