Search code examples
razorforeachtextareanewlineviewdata

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>

Solution

  • 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.