Chris Train
Chris Train

Reputation: 41

First new line is ignored by Razor in TextArea fields

New lines at the start of html fields seem to be ignored by razor.

For example the following new line is ignored.

@{
    var value = "\r\nText";
}

<textarea>@value</textarea>

However if I add a space before the new line, the new line is no longer ignored.

@{
    var value = " \r\nText";
}

<textarea>@value</textarea>

How can I get the first new line to show without included a space before it?

Upvotes: 2

Views: 121

Answers (1)

Chris Train
Chris Train

Reputation: 41

Using the following html helper fixes the problem.

@Html.TextArea("Name", value)

Browsers seem to ignore the first new line due to textarea's commonly being written as:

<textarea>
   Value
</textarea>

Upvotes: 2

Related Questions