Today I am going to explain how to view multi-line string in ASP.NET MVC with line break which I come across recently while working on a Blog site.
Problem:
I have seen lots of web developers have problem to writing the text from a multi-line textbox to the SQL and displaying back in HTML table format. Lets say I am developing a blog site where users can post blogs and view them. Generally the best way to display blogs is the HTML table format. The problem is here the multi-line text with line break will lose all the line break while rendering. To get-rid of this I have found a simple/great way to do this..
Solution:
My input text was like this from a multi-line test box:
a
b
c
d
If I see the above string from C# before rendering to web browser it shows me like this "a\r\nb\r\nc\r\nd" but when it displays in web page, it changes to "a b c d" without line break which I doesn't want. Now to solve this problem I did the following.
InputString.Replace("\r\n", "Put here br")
One thing, Don't use Html.Encode() for this type of issue though it is not rite. Happy programming...
Sunday, March 28, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment