GRU119
GRU119

Reputation: 1128

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>

Upvotes: 7

Views: 9294

Answers (2)

WrqnHrd
WrqnHrd

Reputation: 11

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>
}

Upvotes: 1

Daniel Grim
Daniel Grim

Reputation: 2271

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.

Upvotes: 6

Related Questions