O'Reilly logo

ASP.NET MVC 2 Cookbook by Richard Kimber, Andrew Siemer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reintroducing for and foreach

In this recipe, we will take a look at how to iterate over a collection of data using for and foreach. In this recipe, we will use NBuilder to create a list of Product classes for us to work with. Then we will build three views that will iterate over the collection in different ways. We will show how to iterate over a collection of products pulled from ViewData using foreach. Then we will see how we can iterate over the Model property of a strongly typed view. And finally, we will create another strongly typed view and use a for statement to iterate over the collection.

How to do it...

Initial bits...

  1. The first thing that we need to do is create a new ASP.NET MVC project. Then we will add a reference to NBuilder (in ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required