how do you add a NEW LINE character in the string portion of an ActionLink?
Asked Answered
A

9

7

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.

Alexandra answered 7/8, 2009 at 15:49 Comment(0)
J
3

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.

Jacie answered 7/8, 2009 at 17:53 Comment(0)
B
1

Try this:

<%= Ajax.ActionLink("f<br />lastname", ...more stuff
Baseboard answered 7/8, 2009 at 15:51 Comment(1)
You might have to wrap this in @Html.Raw these days.Dashtilut
A
0

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&lt;br /&gt;lastname</a>

What you could try instead would be a formatting:

<%= string.Format(Ajax.ActionLink("f{0}lastname", ...more stuff), "<br />") %>
Annual answered 7/8, 2009 at 15:55 Comment(2)
Darn doesn't work. That gets me a-- "System.Web.HttpCompileException was unhandled by user code error CS1010: Newline in constant"Alexandra
Hmm, trying using .Replace instead of string.format? I haven't really worked with ajax extensions, so I don't know of another way to do this.Annual
A
0

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";
Ambrogino answered 7/8, 2009 at 17:30 Comment(0)
S
0

Did you try the \r\n combination?

Switchback answered 7/8, 2009 at 17:50 Comment(0)
S
0

How about:

<%= Server.UrlDecode(Ajax.ActionLink(Server.UrlEncode("f<br/>lastname"), ...more stuff
Switchback answered 3/12, 2009 at 16:41 Comment(0)
I
0

This works for me -

<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT &lt;br/> Claim #", "actionName" ))%>
Ignescent answered 10/10, 2011 at 12:52 Comment(0)
D
0

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.

Dashtilut answered 28/1, 2013 at 19:50 Comment(0)
B
0

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.

Beauty answered 23/12, 2016 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.