To use a foreach
loop in Razor syntax within a C# code block, you can use the foreach
keyword followed by parentheses containing the loop variable, the "in" keyword and the source data to iterate over. Inside the block of the loop, you can then access properties or values of the current item using the loop variable.
Here is an example of using foreach
loop in Razor:
main.cs58 chars5 lines
In this example, Model
refers to the source data being passed to the view, and Name
is a property of each item in the source data that is being displayed in a div
tag for each iteration of the loop.
gistlibby LogSnag