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

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

ASP.NET MVC: how to include <span> in link from Html.ActionLink?

asp.net-mvc

提问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 &lt;and &gt;.

它只是 HTML 将<and编码>&lt;and &gt;

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.ActionHtml.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

回答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:推荐使用以前的建议并编写一个帮助程序来结束它。