Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

razor syntax - foreach loop

Tags:

razor

@foreach (string s in "1,2,3".Split(',')) {   s is equal to @s<br/> } 

I want to spit out: s is equal to 1 s is equal to 2 s is equal to 3

But I'm getting all sorts of errors because Visual Studio thinks that what is between the {}'s is code, but I want it to be markup.

like image 522
Ian Davis Avatar asked Dec 16 '10 16:12

Ian Davis


People also ask

How do you use a loop in razor view?

Generally, the loops in asp.net mvc razor view will work same as other programming languages. We can define loop inside or outside code block in the razor and use the same looping concept for assign value, define condition and increment, or decrement value.

What is Razor syntax?

Razor syntax is a simple programming syntax for embedding server-based code in a web page. In a web page that uses the Razor syntax, there are two kinds of content: client content and server code.


1 Answers

Just saw this on ScottGu's blog this morning: use @: before that line:

@foreach (string s in "1,2,3".Split(',')) {   @: s is equal to @s<br/> } 

Alternately, use the <text /> tag:

@foreach (string s in "1,2,3".Split(',')) {   <text>s is equal to @s<br/></text> } 
like image 115
Domenic Avatar answered Sep 21 '22 09:09

Domenic



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!