O'Reilly logo

Professional ASP.NET MVC 4 by Scott Hanselman, K. Scott Allen, Brad Wilson, Phil Haack, Jon Galloway

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

Specifying a View

In the previous section, you looked at examples of what goes inside a view. In this section, you look at how to specify the view that should render the output for a specific action. It turns out that this is very easy when you follow the conventions implicit in the ASP.NET MVC Framework.

When you create a new project template, you'll notice that the project contains a Views directory structured in a very specific manner (see Figure 3.1).

By convention, the Views directory contains a folder per controller, with the same name as the controller, but without the Controller suffix. Thus, for the HomeController, there's a folder in the views directory named Home.

Within each controller folder, there's a view file for each action method, named the same as the action method. This provides the basis for how views are associated to an action method. For example, an action method can return a ViewResult via the View method, as follows:

public class HomeController : Controller {
    public ActionResult Index() {
        ViewBag.Message = "Modify this template to jump-start 
                           your ASP.NET MVC application.";
        return View();
    }
}

This method ought to look familiar; it's the Index action method of HomeController in the default project template.

Notice that unlike the sample in Code Listing 3-2, this controller action doesn't specify the view name. When the view name isn't specified, ...

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