14

View Model:

public class Note
{
    [DataType(DataType.MultilineText)]
    public string Text { get; set; }
}

Default editor template renders a <textarea> element with the newlines preserved.

The default display template renders the text as a single string with the newlines removed.

I tried this, but it doesn't work:

~/Views/Shared/EditorTemplates/MultilineText.cshtml

@model string

@Html.Raw(Model.Replace(System.Environment.NewLine, "<br />"))

I can do something silly like @Html.Raw(Model.Replace("e", "<br />")) and it will work but of course I only want to replace the newline characters the <br /> element! I also tried using @"\n" and that didn't work either.

Any ideas?

Thanks!

2
  • 2
    Of course @"\n" didn't work you need to use "\n" the @ forces the string to be parsed as a literal. Commented Sep 23, 2011 at 0:23
  • That was quite a bone-headed mistake on my part. Commented Sep 23, 2011 at 0:32

6 Answers 6

28

The answer is that you'd do none of this. That's the job of your stylesheet. Basically, render the content any way you want, into a <p>, for example, and use CSS to control how white space is preserved. For example:

(in your style tag, or in your CSS)

p.poem {
   white-space:pre;
}

(in your HTML markup)

<p class="poem">
    There is a place where the sidewalk ends
    And before the street begins,
    And there the grass grows soft and white,
    And there the sun burns crimson bright,
    And there the moon-bird rests from his flight
    To cool in the peppermint wind.
</p>
Sign up to request clarification or add additional context in comments.

6 Comments

I didn't even mention the controller in my post. I'm not sure I understand what you're talking about. I use attributes in my view models to provide metadata about primitive type properties (multilinetext, currency, etc). The metadata is used, as show in my example, by custom razor templates.
Edited accordingly. Instead of putting <pre> or actual style in your ViewModel, render out a class in your view describing what the object being rendered is, not how it is to be formatted.
Do you really think it's better to have a separate class for say, currency, rather than using decimal and using an attribute to specify that the decimal property is currency? In my example the attribute is specifying that the string is a note. The cshtml template describes how to format such a property. I'm not trying to argue but am actually curious. Thanks!
Indeed -- where do you draw the line? I can see being tempted to try something with CSS and currency formatting, but currency is different because you're changing the textual content based on culture. In your case, I'd call the CSS class "note" and that would give you the flexibility to change the color or font of notes without changing the markup. You'd also be able to control notes' behaviors easily with jQuery should you want to. Also, white-space in CSS has a lot more options in it than just straight "pre". You might want to actually use "pre-wrap" or one of those options instead.
I think this is better than using @Html.Row.
|
22

You could try this:

@Html.Raw("<pre>"+ Html.Encode(Model) + "</pre>");

This will preserve your content and show it as-is.

4 Comments

Also Remember to html encode the model value with Html.Encode(string)
@stringValue - does HTML encoding already (Tested in ASP.NET MVC 4). What's the point to do it additionally?
@PhilippMunin Maybe if you checked the date of the answer it would have been obvious that asp.net mvc 4 wasn't available at the time :)
Also if you want to preserve your spaces you can add .Replace(" ", "&nbsp;"). Like this @Html.Raw("<pre>"+ Html.Encode(Model).Replace(" ", "&nbsp;") + "</pre>");
11

i would recommend formatting the output with css instead of using consuming server side strings manipulation like .replace,

just add this style property to render multiline texts :

.multiline
{
   white-space: pre-line;
}

then

<div class="multiline">
  my
  multiline
  text
</div>

newlines will render like br elements.

Comments

5

Try @Html.Raw(Model.Replace("\r\n", "<br />"))

Comments

3
<pre>@myMultiLineString</pre>

OR

<span style="white-space:pre">@myMultiLineString</span>

No need to do Html.Encode, as it's done by default

Comments

1
 [DataType(DataType.MultilineText)]
public your_property {set; get;}

and it will work if your using EditorFor()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.