Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new line within a textarea that's using asp.net razor markup

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>
like image 295
GRU119 Avatar asked Jun 03 '16 18:06

GRU119


2 Answers

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.

like image 169
Daniel Grim Avatar answered Oct 20 '22 21:10

Daniel Grim


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>
}
like image 26
WrqnHrd Avatar answered Oct 20 '22 22:10

WrqnHrd