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.

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
Whozumommy
  • 3,203
  • 7
  • 29
  • 22

9 Answers9

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.

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
1

Try this:

<%= Ajax.ActionLink("f<br />lastname", ...more stuff
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
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 />") %>
Joel
  • 19,175
  • 2
  • 63
  • 83
  • Darn doesn't work. That gets me a-- "System.Web.HttpCompileException was unhandled by user code error CS1010: Newline in constant" – Whozumommy Aug 07 '09 at 16:14
  • 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. – Joel Aug 07 '09 at 17:48
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";
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
jay_t55
  • 11,362
  • 28
  • 103
  • 174
0

Did you try the \r\n combination?

Grinn
  • 5,370
  • 38
  • 51
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.

Umar Farooq Khawaja
  • 3,925
  • 1
  • 31
  • 52
0

How about:

<%= Server.UrlDecode(Ajax.ActionLink(Server.UrlEncode("f<br/>lastname"), ...more stuff
Grinn
  • 5,370
  • 38
  • 51
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.

JohnC.IT
  • 33
  • 5
0

This works for me -

<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT &lt;br/> Claim #", "actionName" ))%>
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
Hemant Tank
  • 1,724
  • 4
  • 28
  • 56