21 Using controllers with views, part I

 

This chapter covers

  • Using controllers with views to programmatically generate HTML content
  • Using the Razor view syntax to mix code and markup
  • Selecting a view in an action method
  • Using view models to pass data from an action to a view

In this chapter, I introduce the Razor view engine, which is responsible for generating HTML responses that can be displayed directly to the user (as opposed to the JSON and XML responses, which are typically consumed by other applications). Views are files that contain C# expressions and HTML fragments that are processed by the view engine to generate HTML responses. I show how views work, explain how they are used in action methods, and describe the different types of C# expressions they contain. In chapter 22, I describe some of the other features that views support. Table 21.1 puts Razor views in context.

21.1 Preparing for this chapter

21.1.1 Dropping the database

21.1.2 Running the example application

21.2 Getting started with views

21.2.1 Configuring the application

21.2.2 Creating an HTML controller

21.2.3 Creating a Razor View

21.2.4 Selecting a View by name

21.3 Working with Razor Views

21.3.1 Setting the view model type

21.3.2 Understanding the view model type pitfall

21.4 Understanding the Razor syntax

21.4.1 Understanding directives

21.4.2 Understanding content expressions

21.4.3 Setting element content

21.4.4 Setting attribute values

21.4.5 Using conditional expressions

21.4.6 Enumerating sequences

21.4.7 Using Razor code blocks

Summary