我正在用 C# 构建一个 Ajax.ActionLink,它开始:
<%= Ajax.ActionLink("f lastname", ...more stuff
我希望在“f”和“lastname”之间有一个换行符。我怎样才能做到这一点?我认为特殊字符是 \n 但这不起作用,<br>
也不起作用。
我正在用 C# 构建一个 Ajax.ActionLink,它开始:
<%= Ajax.ActionLink("f lastname", ...more stuff
我希望在“f”和“lastname”之间有一个换行符。我怎样才能做到这一点?我认为特殊字符是 \n 但这不起作用,<br>
也不起作用。
您可能不得不恢复执行以下操作:
<a href="<%= Url.Action("action") %>">f<br />last</a>
然后手动连接 Ajax 位。
尝试这个:
<%= Ajax.ActionLink("f<br />lastname", ...more stuff
您不能使用<br />
,因为 ActionLink 方法(实际上我相信所有 html 和 ajax 扩展方法)对字符串进行编码。因此,输出将类似于
<a href="...">f<br />lastname</a>
您可以尝试的是格式化:
<%= string.Format(Ajax.ActionLink("f{0}lastname", ...more stuff), "<br />") %>
我认为 Andrew Hare 的回答是正确的。如果您有稍微复杂的需求,您可以选择创建自己的 AjaxHelper 或 HtmlHelper。这将涉及通过执行以下操作来创建适用于 AjaxHelper 和 HtmlHelpers 的自定义扩展方法:
public static class CustomHtmlHelperExtensions
{
public static MvcHtmlString FormattedActionLink(this HtmlHelper html, ...)
{
var tagBuilder = new TagBuilder("a");
// TODO : Implementation here
// this syntax might not be exact but you get the jist of it!
return MvcHtmlString.Create(tagBuilder.ToString());
}
}
您可以使用 dotPeek 或您最喜欢的 .NET 反射工具来检查 ASP.NET MVC(例如 ActionLink)等附带的标准扩展,以了解 Microsoft 如何实现大多数这些扩展方法。他们有一些很好的模式来写这些。过去,我采用这种方法来简化以可读方式输出 HTML,例如,用于 Google Maps 或 Bing Maps 集成,用于创建诸如ActionImage
eg 之类的选项,@Html.ActionImage(...)
或者通过启用诸如@Html.Textile("textile formatted string")
.
如果您在单独的程序集中定义它(就像我一样),请记住将其包含到您的项目引用中,然后将其添加到项目的 Web.config 中。
显然,这种方法对于您的特定目的来说太过分了,因此,我投票赞成 Andrew Hare 针对您的具体案例的方法。
\n
曾经为我工作。但现在它似乎被贬低了。或者,您可以使用 NewLine 方法,例如:
string jay = "This is a" + Environment.NewLine + "multiline" + Environment.NewLine + "statement";
您是否尝试过 \r\n 组合?
这对我有用 -
<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT <br/> Claim #", "actionName" ))%>
怎么样:
<%= Server.UrlDecode(Ajax.ActionLink(Server.UrlEncode("f<br/>lastname"), ...more stuff
问这个问题已经好几年了,但我遇到了麻烦。我发现答案是(在 MVC 中):
ActionLink 中的文本: ...ActionLink("TextLine1" + Environment.Newline + "TextLine2", ...
在 ActionLink 中,有一个类指向带有这一行的 css:
空格:前;
而已。我已经看到他们将整个 Actionline 放在 <pre></pre> 标记中的答案,但这导致的问题多于解决的问题。