How do I add a newline after each item, in a textarea that's inside a Razor @foreach
statement?
The code below displays everything on one line like...
12341524345634567654354487546765
When I want...
12341524
34563456
76543544
87546765
<textarea>
@foreach (var item in ViewData.Model)
{
@item["ACCT_ID"]
}
</textarea>
You can add raw HTML with the HTML helper @Html.Raw()
. In your case, something like this should work:
<textarea>
@foreach (var item in ViewData.Model)
{
@item["ACCT_ID"]
@Html.Raw("\n")
}
</textarea>
This will insert a raw newline character (\n
) after each item.
In your controller:
Simply escape the newline, \n, character in your string.
message = "Violation of UNIQUE KEY constraint" + "\\n";
message += "Customer " + Customer.ToString() + "\\n";
message += "Invoice " + Invoice.ToString();
Viewbag.AlertMessage = message;
In your View:
@if (ViewBag.Alertmessage != null)
{
<script type="text/javascript">
alert("@ViewBag.Alertmessage");
ViewBag.AlertMessage = "";
</script>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With