-1

I discovered that I can use string.Join(", ", string[] enumerable) to turn a list of strings into a single comma delimited string. Could I do something similar to produce for example a list of hyperlinks in a Razor View? Instead of something like:

@foreach(var item in enumerable) 
{
    <a href="@item.Url">@item.Title</a>
    if(item != enumerable.Last()) { <span>, </span> }
}

And if so, would it be advisable or should I just stop being lazy?

Lawyerson
  • 919
  • 1
  • 7
  • 25
  • 2
    Stop being lazy :). You would need to include the html markup in the 'comma delimited string' and use `Html.Raw()` to generate it in the view. –  Jan 27 '16 at 11:01
  • You're right. The accepted answer there is what I was looking for. My bad! – Lawyerson Jan 27 '16 at 11:15

1 Answers1

1
@Html.Raw(string.Join("<span>, </span>", enumerable.Select(item => string.Format("<a href=\"{0}\">{1}</a>", item.Url, item.Title))))
wertzui
  • 5,148
  • 3
  • 31
  • 51