Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 532
Automatico Avatar asked Nov 24 '25 02:11

Automatico


1 Answers

The <text> tag should do it for you.

<p>
@if (someCondition){
   foreach (Sometype s in Model.ListOfSomeTypes){
      @s.Name<text>, </text>
   }
}
</p>
like image 61
Gromer Avatar answered Nov 25 '25 17:11

Gromer



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!