Chapter 3. View fundamentals

 

This chapter covers

  • Providing data to the view
  • Using strongly typed views
  • Understanding view helper objects
  • Developing with templates

The view’s responsibility can be deceptively simple. Its goal in life is to take the model given to it and use it to render content. Because the controller and related services already executed all the business logic and packaged the results into a model object, the view only needs to know how to take that model and turn it into HTML. Although this separation of concerns removes much of the responsibility that can plague traditional ASP.NET applications, views still need to be carefully and deliberately designed. Views require knowledge and understanding of the building blocks of the web, including HTML, CSS, and JavaScript.

In this chapter, we’ll examine how ASP.NET MVC renders views, how the default WebFormViewEngine functions, and how to structure and organize views. Then we’ll look at a couple of approaches for using the model to render content in a view. Finally, we’ll cover the templating features new to ASP.NET MVC 2.

3.1. Introducing views

A view’s responsibility is to render content. But how does the MVC framework decide which view to use? How do we control what gets rendered, and how do we organize our content? How do we even tell MVC to render a view?

3.2. Examining the ViewDataDictionary

3.3. Strongly typed views with a view model

3.4. Displaying view model data in a view

3.5. Using strongly typed templates

3.6. Summary