maff
maff

Reputation: 773

Show new lines from text area in ASP.NET MVC

I'm currently creating an application using ASP.NET MVC. I got some user input inside a textarea and I want to show this text with <br />s instead of newlines. In PHP there's a function called nl2br, that does exactly this. I searched the web for equivalents in ASP.NET/C#, but didn't find a solution that works for me.

The fist one is this (doesn't do anything for me, comments are just printed without new lines):

<%
    string comment = Html.Encode(Model.Comment);
    comment.Replace("\r\n", "<br />\r\n");
%>
<%= comment %>

The second one I found was this (Visual Studio tells me VbCrLf is not available in this context - I tried it in Views and Controllers):

<%
    string comment = Html.Encode(Model.Comment);
    comment.Replace(VbCrLf, "<br />");
%>
<%= comment %>

Upvotes: 10

Views: 21084

Answers (6)

Ajay2707
Ajay2707

Reputation: 5808

I have the same issue and above all answer given the hint, not exactly help, that's why given the answer to help.

Replace "\n" strign with '
', '\r\n','\n', nothing help and finally when replace "\r\n", it will work. (in display it like \n, but in database it store as \\n)

Old code

 @Html.TextArea("MEPat_textPatNote", Model.MedicationPatCur, 4, 4, new { @class = "k-textbox", style = "width: 100%;", maxlength = "300" })

enter image description here

New code

 @Html.TextArea("MEPat_textPatNote", Model.MedicationPatCur.PatNote== null? "": Model.MedicationPatCur.PatNote.Replace("\\n","\r\n"), 4, 4, new { @class = "k-textbox", style = "width: 100%;", maxlength = "300" })

enter image description here

Upvotes: 0

Mizanul Islam
Mizanul Islam

Reputation: 11

If you have a Razor-based view , string with line breaks and want to show that text with the line-breaks intact in your view, you can do this without replacing all \r\n with "html br"-tags. Instead present the text in an element that has the style property white-space set to pre-line. You should really add a class like:

<span class="line-breaks">@Model.MyText</span>


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

Original Found @

https://kaliko.com/blog/text-line-breaks-in-asp.net-mvc-razor-view/

Upvotes: 1

Arif Dewi
Arif Dewi

Reputation: 2272

@Html.Raw(@Model.Comment.RestoreFormatting())

and than...

public static class StringHelper
{
    public static string RestoreFormatting(this string str)
    {
        return str.Replace("\n", "<br />").Replace("\r\n", "<br />");
    }
}

Upvotes: 0

Mohammad Bani Hani
Mohammad Bani Hani

Reputation: 31

to view html tags like a DisplayFor

you need to use another method , in fact the mvc dosent allowed you to view tags in page

but you can used this to ignore this option

@Html.Raw(model => model.text)

good luck

Upvotes: 3

eu-ge-ne
eu-ge-ne

Reputation: 28153

Try (not tested myself):

comment = comment.Replace(System.Environment.NewLine, "<br />");

UPDATED:

Just tested the code - it works on my machine

UPDATED:

Another solution:

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringReader sr = new System.IO.StringReader(originalString);
string tmpS = null;
do {
    tmpS = sr.ReadLine();
    if (tmpS != null) {
        sb.Append(tmpS);
        sb.Append("<br />");
    }
} while (tmpS != null);
var convertedString = sb.ToString();

Upvotes: 26

nvtthang
nvtthang

Reputation: 604

Please have a look this answer Replace Line Breaks in a String C# here.

Upvotes: 0

Related Questions