문제

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 = ",";
}
도움이 되었습니까?

해결책

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 = ",";
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top