Iterating in a Template
Letβs revisit part of the non-efficient HTML from the introduction, where we hard-coded coffee options in a list.
|
|
If we want to add, remove, or edit the list items, we need to go in and change the individual tags, which is a poor use of our time. Fortunately, there is a way to streamline the process.
In C#, we use a foreach Loop to iterate through the items in a data collection.
|
|
Razor templates allow us to use a foreach
loop to display items in a collection.
|
|
Let’s explore line 2 to better understand how we are using the foreach
loop in the Razor View.
- The
@
specifies the C# portion of the template. - The
@foreach
loop is initiated inside of a list element (either<ol>
or<ul>
). collectionItem
represents an individual item or element within the collection.ViewBag.collectionProperty
represents any collection that has been assigned as a property onViewBag
.
We can think of this syntax as saying, βFor each item within the ViewBag
property, collectionProperty
, repeat this HTML tag, but use the current value of collectionItem
.β
Letβs apply this new concept to the HTML coffee list example. Assume that we store each of the coffee names as strings in a List
called ViewBag.coffeeOptions
.
|
|
Some points to note:
coffeeOptions
is accessible to the template because it is a property of theViewBag
object.In the first pass through the loop,
coffeeType
takes the value of the first coffee name in thecoffeeOptions
list.@coffeeType
displays the value ofcoffeeType
in the view, so theli
element will show the first coffee name.Each successive iteration,
coffeeType
takes the next value in the list, and Razor adds a new<li></li>
element to display that data.
@foreach
creates one HTML tag for each item in a collection. BE CAREFUL where you place it.
Consider the following Razor code:
|
|
The final HTML produced is one heading, 4 ordered lists, and 4 coffee names. When this view is rendered, each coffee type is labelled with β1β.
|
|
Nested Loops
Assume you have a collection of different CoffeeShop
objects. Each object contains a string field for name
and a field that is a list of of the brews available, coffeeOptions
.
Below, we nest loops to display a list of the shop names and their brew options.
Sample Razor template:
|
|
Sample HTML output:
|
|
Apart from the nested loops displayed above, here are some other items you may find useful to note from the example above.
Razor comments are seen on lines 3 and 8 in the first code block above. Comments in Razor are nested between
@*
and*@
. You may have noticed the comment block present on the top of a new view file.ViewBag.coffeeShops
is a list ofCoffeeShop
objects but weβve used var on line 1 to type the coffeeShop item.In some limited circumstances, we can use the var keyword to implicitly type a variable. When this keyword is used, C# still assigns a type to
coffeeShop
through inference. It looks and sees that we are assigningcoffeeShop
to the value at the list index, which is aCoffeeShop
object. Thus,coffeeShop
is of typeCoffeeShop
.Alternatively, Razor does also allow us to import a custom class, such as CoffeeShop. If we wanted to do so, we could import the class or its namespace at the top of the template with a using statement.
We use var
above to simplify the example and focus on the loop action. In general, we recommend that you avoid using var
while you are learning C#. Even after you become more experienced with the language, you will still only want to use it sparingly and in specific circumstances. Explicitly declaring the type of your variables makes for more readable code, to name only one benefit.
Check Your Understanding
What is the HTML outcome you expect from the Razor code below?
|
|
- One heading, 4 ordered lists, and 4 coffee names (each name labeled as β1β)?
- One heading, one ordered list, and 4 coffee names (with the names labeled β1β, β2β, β3ββ¦)?
- 4 headings, 4 ordered lists, and 4 coffee names (each name labeled as β1β)?
- 4 headings, 4 ordered lists, and 16 coffee names (with the names labeled β1β, β2β, β3ββ¦)?
What is the HTML outcome you expect from the Razor code below?
|
|
- One heading, 4 ordered lists, and 4 coffee names (each name labeled as β1β)?
- One heading, one ordered list, and 4 coffee names (with the names labeled β1β, β2β, β3ββ¦)?
- 4 headings, 4 ordered lists, and 4 coffee names (each name labeled as β1β)?
- 4 headings, 4 ordered lists, and 16 coffee names (with the names labeled β1β, β2β, β3ββ¦)?