
In this chapter, we will cover:
- Using magic strings and the
ViewData
dictionary - Creating a strongly typed view
- Decoupling a strongly typed view with a View Model
- Centralizing display logic with templated helpers
- Using a partial view to segment view code
- Rendering a child view with
Html.RenderAction
- Building a view from the database with NVelocity
- Consuming a JSON view with jQuery
In this chapter, we will take a look at the easiest part of the whole ASP.NET MVC framework—the View. We will get started by looking at how data is passed out to the view in the simplest of ways using the ViewData
dictionary and magic strings. From there, we will work towards better ways of getting data in and out of the view. Then we will take a look at some of the other view engines that you have at your disposal, which can easily be plugged into the ASP.NET MVC framework. Finally, we will take a look at other things that you can do with the view.
In our examples, we will be working with the basic concept of products and categories as though we were building a simple e-commerce site. We will be using an open source product called NBuilder to fake our data access layer. NBuilder will be used to quickly generate some product and category data for us to use in our views.
Note
NBuilder is a great open source product that can be used to quickly generate test instances of a particular .NET class. This works great when knocking together your views such as we are doing in this chapter. You can find more data about NBuilder at NBuilder.org. I also wrote a tutorial on how to get NBuilder running on DotNetSlackers.com at dotnetslackers.com/articles/aspnet/Using-NBuilder-to-mock-up-a-data-driven-UI-Part1.aspx.