Reputation: 15488
I'm having trouble wrestling with inserting spaces in commas in a foreach loop. Is there a better way to do this? It bother
@{ var multipleSpeakerSeparator = " "; }
@foreach (var speaker in session.SpeakersList)
{
@multipleSpeakerSeparator <a href="@(speaker.SpeakerLocalUrl)">
@speaker.UserFirstName @speaker.UserLastName </a>multipleSpeakerSeparator = ",";
}
Upvotes: 0
Views: 272
Reputation: 50728
Why not use String.Join:
String.Join(",", session.SpeakersList.Select(i => "<a href=" +
i.SpeakerLocalUrl + ">..</a>");
I don't know if you can use @ syntax within String.Join, and how it would work, and how that would work with String.Join. Otherwise, using string concatenation as shown above would work.
If you are trying to inject a space, a space literal should render appropriately. I'm surprised it does not. Anyway, using the foreach approach, you should be able to do @<text> </text>
, and could conditionally do:
@for (var i = 0; i < session.SpeakersList.Count; i++)
{
@{
if (i > 0) { <text> </text> }
}
<a href="@(speaker.SpeakerLocalUrl)">
@speaker.UserFirstName @speaker.UserLastName </a>multipleSpeakerSeparator = ",";
}
Upvotes: 1