What a View Does

The view is responsible for providing the user interface (UI) to the user. It is given a reference to the model, and it transforms that model into a format ready to be presented to the user. In ASP.NET MVC, this consists of examining the ViewDataDictionary handed off to it by the Controller (accessed via the ViewData property) 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. HTML is the language of the web. But as the section on action results later in this chapter points out, views can render other content types as well.

Starting in ASP.NET MVC 3, view data can also be accessed via the ViewBag property. ViewBag is a dynamic property that provides a convenient syntax for accessing the same data accessible via the ViewData property. It's effectively a wrapper over ViewData that takes advantage of the new dynamic keyword in C# 4. This allows using property accessor-like syntax to retrieve values from a dictionary.

Thus ViewBag.Message is equivalent to ViewData[“Message”].

For the most part, there isn't a real technical advantage to choosing one syntax over the other. ViewBag is just syntactic sugar that some people prefer over the dictionary syntax.

note

While there isn't a real technical advantage to choosing one format over there other, there are some critical differences to be aware of between the two syntaxes.

One obvious one is that ViewBag only works when the key being accessed is a valid C# identifier.

For example, if we place a value in ViewData[“Key With Spaces”], we can't access that value using ViewBag.

Another key issue to be aware of is that dynamic values cannot be passed in as parameters to extension methods. The C# compiler must know the real type of every parameter at compile-time in order for it to choose the correct extension method.

If any parameter is dynamic then compilation will fail. For example, this code will always fail: @Html.TextBox(“name”, ViewBag.Name). The ways to work around this are to either use ViewData[“Name”] or to cast the value to a specific type:(string)ViewBag.Name.

In the case of a strongly typed view, which is covered in more depth later, the ViewDataDictionary has a strongly typed model object that the view renders. This model might represent the actual domain object, such as a Product instance, or it might be a presentation model object specific to the view, such as a ProductEditViewModel instance. For convenience, this model object can be referenced by the view's Model property.

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:

UnFigure
@{
    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>

Code snippet 3-1.txt

This is an extremely simple example of a view that displays a message (via the @ViewBag.Message expression) set by the controller. When this view is rendered, that expression is replaced with the value we set in the controller and output as HTML markup.

One important thing to note, unlike ASP.NET Web Forms and PHP, is that 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 that the view will render. Let's look at one possible controller that might have initiated this view:

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

Code snippet 3-2.txt

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 we saw in Code Snippet 3-1. That view will display the value of ViewBag.Message that was passed to it. This is just one way to pass data to a view. In the section “Strongly Typed Views,” we'll look at another approach to passing data to a view.

If you've used ASP.NET MVC in the past, you'll notice that this view looks dramatically different than the views you're used to. This is a result of the new Razor syntax included in ASP.NET MVC 3.

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

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