如何在 ActionLink 的字符串部分添加 NEW LINE 字符?
我正在 C# 中构建一个 Ajax.ActionLink,它的开头是:
<%= Ajax.ActionLink("f lastname", ...more stuff
我希望在单词“f”和“lastname”之间有一个换行符。 我怎样才能做到这一点? 我认为特殊字符是 \n 但这不起作用,并且
也不起作用。
I'm building a Ajax.ActionLink in C# which starts:
<%= Ajax.ActionLink("f lastname", ...more stuff
and I'd like there to be a new line character between the words "f" and "lastname". How can I accomplish this? I thought the special character was \n but that doesn't work, and <br>
doesn't work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这个问题已经问了好几年了,但我却遇到了麻烦。 我发现答案是(在 MVC 中):
ActionLink 中的文本: ...ActionLink("TextLine1" + Environment.Newline + "TextLine2", ...
在 ActionLink 中,有一个指向 css 的类line:
whitespace: pre;
就是这样,他们将整个 Actionline 放在 < pre > 标签中,但这导致的问题比它解决的问题还要多。
It's been several years since the question was asked, but I had trouble with it. I found the answer to be (in MVC):
Text in your ActionLink: ...ActionLink("TextLine1" + Environment.Newline + "TextLine2", ...
In the ActionLink, have a class that points to a css with this line:
whitespace: pre;
That's it. I've seen answers where they put the entire Actionline in < pre > < /pre > tags, but that caused more problems than it solved.
我认为安德鲁·黑尔的答案是正确的。 如果您有稍微复杂的需求,您可以选择创建自己的 AjaxHelper 或 HtmlHelper。 这将涉及创建适用于 AjaxHelper 和 HtmlHelpers 的自定义扩展方法,方法如下:
您可以使用 dotPeek 或您最喜欢的 .NET 反射工具来检查 ASP.NET MVC 附带的标准扩展(例如 ActionLink)等以查找Microsoft 如何实现大部分扩展方法。 他们有一些很好的模式来编写这些内容。 过去,我采用这种方法以可读的方式简化 HTML 输出,例如,用于 Google 地图或 Bing 地图集成,用于创建
ActionImage
等选项,例如@Html.ActionImage (...)
或通过启用诸如@Html.Textile("textile formatted string")
之类的语法来集成输出 Textile-formatting HTML。如果您在单独的程序集中定义它(就像我一样),请记住将其包含到您的项目引用中,然后将其添加到项目的 Web.config 中。
显然,这种方法对于您的特定目的来说是过度的,因此,我投票支持安德鲁·黑尔针对您的具体案例的方法。
I think Andrew Hare's answer is correct. If you have slightly more complicated requirement, you do have the option to create your own AjaxHelper or HtmlHelper. This will involve creating custom extension methods that work on AjaxHelper and HtmlHelpers, by doing something like:
You can use dotPeek or your favorite .NET reflection tool to examine the standard extensions that come with ASP.NET MVC (e.g., ActionLink) etc to find how Microsoft has implemented most of those extension methods. They have some pretty good patterns for writing those. In the past, I have taken this approach to simplify outputting HTML in a readable manner, such as, for Google Maps or Bing Maps integration, for creating options like
ActionImage
e.g.,@Html.ActionImage(...)
or to integrate outputting Textile-formatting HTML by enabling syntax such as@Html.Textile("textile formatted string")
.If you define this in a separate assembly (like I do), then remember to include this into your project references and then add it to the project's Web.config as well.
Obviously, this approach is overkill for your specific purposes, and for this reason, my vote is for Andrew Hare's approach for your specific case.
\n
曾经为我工作过。 但现在看来已经贬值了。 或者,您可以使用 NewLine 方法,例如:The
\n
used to work for me. But now it seems to be depricated. Alternitavely, you may use the NewLine method, for example:这对我有用 -
This works for me -
怎么样:
How about:
您尝试过 \r\n 组合吗?
Did you try the \r\n combination?
您不能使用
因为 ActionLink 方法(实际上我相信所有 html 和 ajax 扩展方法)都会对字符串进行编码。 因此,输出将类似于您可以尝试的格式:
You can't use
<br />
because the ActionLink method (and indeed I believe all the html and ajax extension methods) encode the string. Thus, the output would be something likeWhat you could try instead would be a formatting:
尝试这个:
Try this:
您可能必须恢复执行类似以下操作:
然后手动连接 Ajax 位。
You might have to revert to doing something like:
And then wire in the Ajax bits manually.