Search code examples
asp.net-mvcrazor

Printing single comma (or other char) in Razor view


I have a seemingly very small problem, but I can't find a solution for it:

Within a Razor view i have something like this:

<p>
@if (someCondition){
   foreach (Sometype s in Model.ListOfSomeTypes){
      @s.Name @//There is a space in front here.
   }
}
</p>

But I want to have to have all the names comma seperated, so i essentially want to do something like this:

<p>
@if (someCondition){
   foreach (Sometype s in Model.ListOfSomeTypes){
      @s.Name, 
   }
}
</p>

However, Razor freaks out. How can I achieve this kind of result?


I have tried some variations like this:

@{','} @s.Name @//No good
@"," @s.Name @//No good
@{","} @s.Name @//No good
@StringVariableContainingComma @s.Name @//This works, but seems to be very unnessecary to me.

Ps. I know that the above suggested solution for comma seperated elemens don't work propperly. Removed conditions for easier reading of question.


Solution

  • Use regular brackets to specify an explicit code nugget.

    @(s.Name), @(...)