Taking a property on the page called EnquiryNotes
public string EnquiryNotes{get;set;}
This property is hydrated from the database and contains text entered from a multi-line textbox on a webpage. The string from the db is:
"Customer asked for:\r\nadditional functions\r\nongoing support\r\nclear time frame"
Note the newline chars '\r\n' as they clicked enter in the textbox for new lines. We now want to write EnquiryNotes out to a webpage using razor. Let's try:
@Model.Data.EnquiryGeneralNotes;
Viewing the browser this gives us: Customer asked for: additional functions ongoing support clear time frame
This doesn't work because new lines aren't rendered as html line breaks. The newlines in the string need to be replaced with html line breaks <br />. Let's try:
@Model.Data.EnquiryGeneralNotes.Replace(Environment.NewLine, "<br />");
Viewing the browser this gives us: Customer asked for:<br />additional functions<br />ongoing support<br />clear time frame
This doesn't work because razor has escaped the html line breaks. Let's try:
@Html.Raw(Model.Data.EnquiryGeneralNotes.Replace(Environment.NewLine, "<br />"));
Viewing the browser this gives us: Customer asked for
additional functions
ongoing support
clear time frame
Using html.raw meant razor didn't escape the string and wrote the html as expected. Smashing.
#Razor