Chapter 10. Advanced view techniques

 

This chapter covers

  • Using master pages to craft site-wide templates
  • Applying partials for shared snippets of content
  • Leveraging child actions for common widgets
  • Building parameter lists for generating URLs
  • Examining the Spark view engine

The MVC pattern gives us separation of concerns between the model, controller, and view, but this pattern didn’t eliminate the need for developers to carefully design their views. With the elimination of code-behind and the addition of a view model object, we can focus strictly on rendering content inside our view. But without careful attention, our views can still slide into a morass of duplication and spaghetti code. We can no longer lean on custom controls to encapsulate view behavior as we did in Web Forms. Instead, ASP.NET MVC provides similar and expanded mechanisms for tackling all levels of duplication in our views.

First, we’ll look at the various forms of duplication we encounter in our views and explore various means of tackling duplication as it arises.

10.1. Eliminating duplication in the view

In ASP.NET MVC, the ability to use web controls to encapsulate complex UI elements is all but gone. We can use web controls that don’t take advantage of ViewState, but that renders web controls built for Web Forms mostly useless. Instead, we have to turn to other means to eliminate duplication in our views.

With the release of ASP.NET MVC 2, our choices for tackling view duplication are expanded:

10.2. Building query-string parameter lists

10.3. Exploring the Spark view engine

10.4. Summary