-1

I try to separate a list in Razor by "," but I don't want the "," at the end. I try to use String.join but nothing is happening.

This is my code:

@foreach (Language language in Model.apiAgent.languages)
{   
    @(string.Join(",", language.language));
}

it is still shows as

EnglishSpanish

T.S.
  • 18,195
  • 11
  • 58
  • 78
nik
  • 514
  • 2
  • 6
  • 19
  • [msdn-docs](https://msdn.microsoft.com/de-de/library/57a79xd0(v=vs.110).aspx) aren't that aweful anymore ;) String.Join(String, String[]) You need to use your List/Array/Collection as second param. – nilsK Apr 11 '18 at 20:04

2 Answers2

7

If you're joining everything into one string, you don't need the loop. Just use

@(string.Join(",", Model.apiAgent.languages));

If the elements contained in languages aren't the strings you want, you may need to tease out the property you want with Select. For example, if each element has a language property that you want to output, you can use:

@(string.Join(",", Model.apiAgent.languages.Select( l => l.language )));
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • without the loop? when I use this, this will print: Mvc.Models.AgentModels.api_Agent+Language,Mvc.Models.AgentModels.api_Agent+Language; – nik Apr 11 '18 at 20:07
  • I see, you need to get the `language` property of each element in the `languages` list. Is that right? Edited my naswer. – John Wu Apr 11 '18 at 20:16
3

It is very hard to tell based off of your question as you provide no real insight into what your model looks like. My guess is that this is what you are looking for though

string.Join(",", Model.apiAgent.languages)
peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76