如何在 ActionLink 的字符串部分添加 NEW LINE 字符?

发布于 2024-07-30 00:17:22 字数 220 浏览 7 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

天暗了我发光 2024-08-06 00:17:33

这个问题已经问了好几年了,但我却遇到了麻烦。 我发现答案是(在 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.

記柔刀 2024-08-06 00:17:32

我认为安德鲁·黑尔的答案是正确的。 如果您有稍微复杂的需求,您可以选择创建自己的 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 地图或 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:

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());
    }
}

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.

最终幸福 2024-08-06 00:17:31

\n 曾经为我工作过。 但现在看来已经贬值了。 或者,您可以使用 NewLine 方法,例如:

string jay = "This is a" + Environment.NewLine + "multiline" + Environment.NewLine + "statement";

The \n used to work for me. But now it seems to be depricated. Alternitavely, you may use the NewLine method, for example:

string jay = "This is a" + Environment.NewLine + "multiline" + Environment.NewLine + "statement";
亽野灬性zι浪 2024-08-06 00:17:28

这对我有用 -

<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT <br/> Claim #", "actionName" ))%>

This works for me -

<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT <br/> Claim #", "actionName" ))%>
触ぅ动初心 2024-08-06 00:17:27

怎么样:

<%= Server.UrlDecode(Ajax.ActionLink(Server.UrlEncode("f<br/>lastname"), ...more stuff

How about:

<%= Server.UrlDecode(Ajax.ActionLink(Server.UrlEncode("f<br/>lastname"), ...more stuff
书间行客 2024-08-06 00:17:26

您尝试过 \r\n 组合吗?

Did you try the \r\n combination?

妄司 2024-08-06 00:17:25

您不能使用
因为 ActionLink 方法(实际上我相信所有 html 和 ajax 扩展方法)都会对字符串进行编码。 因此,输出将类似于

<a href="...">f<br />lastname</a>

您可以尝试的格式:

<%= string.Format(Ajax.ActionLink("f{0}lastname", ...more stuff), "<br />") %>

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 like

<a href="...">f<br />lastname</a>

What you could try instead would be a formatting:

<%= string.Format(Ajax.ActionLink("f{0}lastname", ...more stuff), "<br />") %>
初心未许 2024-08-06 00:17:24

尝试这个:

<%= Ajax.ActionLink("f<br />lastname", ...more stuff

Try this:

<%= Ajax.ActionLink("f<br />lastname", ...more stuff
ら栖息 2024-08-06 00:17:24

您可能必须恢复执行类似以下操作:

<a href="<%= Url.Action("action") %>">f<br />last</a>

然后手动连接 Ajax 位。

You might have to revert to doing something like:

<a href="<%= Url.Action("action") %>">f<br />last</a>

And then wire in the Ajax bits manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文