Skip to main content
Home  › ... Razor

Basic Tutorials

Tutorial HomeBasics

Using a foreach - the most common way of looping

  • dog
  • cat
  • mouse

<ul>
@foreach(var pet in pets) {
  <li>@pet</li>
}
</ul>

Basic for

  • dog
  • cat
  • mouse

<ul>
@for(var i = 0; i < pets.Length; i++) {
  <li>@pets[i]</li>
}
</ul>

Using a for index with two lists (arrays)

  • dog - owned by Daniel
  • cat - owned by John
  • mouse - owned by Markus

<ul>
@for(var i = 0; i < pets.Length; i++) {
  <li>@pets[i] - owned by @owners[i]</li>
}
</ul>

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

@inherits Custom.Hybrid.Razor12
<!-- unimportant stuff, hidden -->
@{
  var pets = new string[] { "dog", "cat", "mouse"};
  var owners = new string[] { "Daniel", "John", "Markus"};
}

Loops - for and foreach @Html.Partial(... <!-- unimportant stuff, hidden -->

<ul>
@foreach(var pet in pets) {
  <li>@pet</li>
}
</ul>




Basic for

<ul>
@for(var i = 0; i < pets.Length; i++) {
  <li>@pets[i]</li>
}
</ul>



Using a for index with two lists... <!-- unimportant stuff, hidden -->

<ul>
@for(var i = 0; i < pets.Length; i++) {
  <li>@pets[i] - owned by @owners[i]</li>
}
</ul>


<!-- unimportant stuff, hidden -->