noclist noclist - 4 months ago 31
HTML Question

preserve line breaks between markup

I am dynamically generating a list of links from SiteCore using a few lines of razor. My problem is the markup generated isn't correctly formatted with line breaks and I'm having some text-wrapping issues because of it.

How the markup is generated:



@foreach (Item child in items[0].Children)
{
@Html.Sitecore().Field("Link To Page", child)
}




The markup being generated:

<nav class="service__nav">

<a href="/">Link 1</a><a href="/">Link 2</a><a href="/">Link 3</a><a href="/">Link 4</a><a href="/">Link 5</a><a href="/">Link 6</a><a href="/">Link 7</a><a href="/">Link 8</a> </nav>


What I need it to be:

<nav class="service__nav">
<a href="/">Link 1</a>
<a href="/">Link 2</a>
<a href="/">Link 3</a>
<a href="/">Link 4</a>
<a href="/">Link 5</a>
<a href="/">Link 6</a>
<a href="/">Link 7</a>
<a href="/">Link 8</a>
</nav>


Is there a way I can add something to end of the each link that will give me a line break in the HTML? I don't want break tags because the HTML itself is fine, just not formatted correctly.

Answer

You can force a line break in the markup by using Environment.NewLine

@foreach (Item child in items[0].Children)
{
        @Html.Sitecore().Field("Link To Page", child) @Environment.NewLine
}

https://msdn.microsoft.com/en-us/library/system.environment.newline(v=vs.110).aspx