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?
Use a string[]
to hold your errors. That way they are a well-formed and distinct set of errors instead of just one long string.
In your Controller, initializing the ViewBag
property:
ViewBag.Errors = new string[] { "First error", "Second error" };
In your View, displaying these errors:
@foreach (string error in ViewBag.Errors)
{
@Html.Label(error)
<br />
}
You shouldn't be handling markup layout within your Controller (i.e. line breaks, or any other DOM elements). The presentation should be handled solely by the View. Which is why it would be best to pass a string[]
.