hellogoodnight
hellogoodnight

Reputation: 2129

Razor removes new line in resource file, is there any way to keep it?

Edit: I added style="white-space: pre-line" to the element in question, that solved my problem.

I have a string in a resource file that looks like this:

Some text.

Some other text.

When I use it like this:

<td>@(Tjuhu.App_GlobalResources.Wie.myLabel)</td>

The output on my page is

Some text. Some other text.

Is there any way to get around this?

Upvotes: 3

Views: 1737

Answers (3)

Jinesh Jain
Jinesh Jain

Reputation: 1242

use < br/> tag inside your resource file

in Resource file myLable="Some string < br /> is here"

At MVC View

@Html.Raw(Tjuhu.App_GlobalResources.Wie.myLabel)

Upvotes: 4

bstenzel
bstenzel

Reputation: 1241

The line breaks in the string are written to the markup. You will be able to see them in the html source code. Of course line breaks have no semantic meaning in HTML, so you don't see them in the rendered page. ;)

What you can do is write into the view @Html.Raw(Tjuhu.App_GlobalResources.Wie.myLabel.Replace("\n", "<br>")

Although it'd be cleaner to write a HtmlHelper extension method for it. Also be aware that HTML tags have a semantic meaning. br is for line breaks within a sentence. So depending on the content, the way to go may very well be to split the string up and put the split parts into p tags.

And for the love of everything that is good and proper: do not put HTML directly into the resource strings! Mixing content and concrete markup will come back to bite you. Or even worse somebody else who in turn will bite you.

Upvotes: 1

Dan
Dan

Reputation: 1018

This is not an issue wirh razor, rather this is an issue with your understanding of how HTML renders.

You can add a style attribute to your <td> tag to preserve line breaks. This would be the simplest method.

<td style="white-space: pre-line">@(Tjuhu.App_GlobalResources.Wie.myLabel)</td>

Alternatively you will need to run your text value through a method to replace line-breaks and replace them with <br> tags to preserve formatting.

Upvotes: 3

Related Questions