MVC3 putting a newline in ViewBag text

MikeTWebb picture MikeTWebb · Feb 8, 2012 · Viewed 13.5k times · Source

I have an MVC3 C#.Net web app. I am looping through a DataTable. Some rows are importing OK, some are not. I am wanting to send a list of errors back to the view in a list format. I am assigning the following text to a ViewBag property

Input error on Row(1) Cell(3) Input string was not in a correct format.<br/> Input error on Row(4) Cell(3) Input string was not in a correct format.<br/>

I was hoping the br would write a line break in the HTML. It does not. I want the error message to look like this

Input error on Row(1) Cell(3) Input string was not in a correct format.
Input error on Row(4) Cell(3) Input string was not in a correct format.

Any ideas?

Answer

devstruck picture devstruck · Feb 8, 2012

When you throw into your view, use

@Html.Raw(ViewBag.Test)

instead of

@ViewBag.Test

That will signify to the compiler that string is html and does not need to be encoded as such.