Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a comma between each item in foreach, @html.displayfor

Tags:

html

c#

I want to add a comma after each language.Language.NativeName Right now they all just come out in one straight line. I cant just add a comma in the NativeName get method because i will use it for other stuff aswell.

<td class=" ">@Html.DisplayFor(modelItem => item.Email)</td>
<td class=" ">@Html.DisplayFor(modelItem => item.Country.Name)</td>
<td class=" ">@Html.DisplayFor(modelItem => item.City)</td>
<td class=" ">
@foreach (UserLanguage language in item.Languages)
{
 @Html.DisplayFor(modelItem => language.Language.NativeName)
}
</td>
<td class=" ">Danish, English, Spanish</td>
like image 887
hosdal Avatar asked Jan 21 '26 02:01

hosdal


1 Answers

Use string.Join:

<td class=" ">@String.Join(", ", item.Languages.Select(p => p.Language.NativeName).ToArray())</td>
like image 156
andyb952 Avatar answered Jan 22 '26 16:01

andyb952



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!