The Purpose of Views

Chapter 2 demonstrated how controllers can return strings, which are then output to the browser. That's useful for getting started with controllers, but in any non-trivial web application, you'll notice a pattern emerging very quickly: Most controller actions need to display dynamic information in HTML format. If the controller actions are just returning strings, they'll be doing a lot of string substitution, which gets messy fast. A templating system is clearly needed, which is where the view comes in.

The view is responsible for providing the user interface (UI) to the user. It is given a reference to the model (the information the controller needs displayed), and the view transforms that model into a format ready to be presented to the user. In ASP.NET MVC, the view accomplishes this by examining a model object handed off to it by the controller and transforming the contents of that to HTML.


Note
Not all views render HTML. HTML is certainly the most common case when building web applications. But, as the section on action results in Chapter 16 points out, views can render a wide variety of other content types as well.

Let's take a quick look at an example of a view. The following code sample shows a view named Sample.cshtml located at the path /Views/Home/Sample.cshtml. Don't worry about typing this; it's just for illustration.

Listing 3-1: Sample view — Sample.cshtml

@{
   Layout = null;
}
<!DOCTYPE html>
<html>
  <head><title>Sample View</title></head>
  <body>
    <h1>@ViewBag.Message</h1>
    <p>
   This is a sample view. It's not much to look at,
     but it gets the job done.
    </p>
  </body>
</html>

This is an extremely simple example of a view that displays a message set by the controller via the @ViewBag.Message expression. You'll learn more about ViewBag and other methods for passing information to the view later in this chapter. When this view is rendered, that expression is replaced with the value we set in the controller and output as HTML markup.

Unlike file-based web frameworks such as ASP.NET Web Forms and PHP, views are not themselves directly accessible. You can't point your browser to a view and have it render. Instead, a view is always rendered by a controller that provides the data the view will render. Let's look at one possible controller that might have initiated this view:

Listing 3-2: Home Controller — HomeController.cs

public class HomeController : Controller {
   public ActionResult Sample() {
       ViewBag.Message = "Hello World. Welcome to ASP.NET MVC!";
       return View("Sample");
   }
}

Notice that the controller sets the ViewBag.Message property to a string and then returns a view named Sample. That will correspond to Sample.cshtml, which you saw in Listing 3-1. That view will display the value of ViewBag.Message that was passed to it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset