number of objects forwarded to the view increase, it is easy to create some helpers in the view to avoid casting
operations everywhere. Many controllers will only forward one object to the view, and for those cases, we can
make use of the
ViewData.Model
property as well as strongly-typed views. For our next example, we will make
a small change to our controller and view to enable a strongly-typed reference. Listing 1.11 shows our controller
discarding the use of the
IDictionary<object, object>
and passing domain model object directly to the
View()
method. This works well for many scenarios, and it even scales to large application with the use of a
"view model", which is a presentation object specifically crafted for use in a single view. For composed views using
ViewData
as a dictionary may is necessary, but partial views, covered in chapter 4, can also provide for composed
views.
For this view, since our model is a
string
, the only difference is the lack of the need to call
ToString()
in
the view. Note in listing 1.11 the simplified rendering. If I want to call
ToLower()
to ensure the text was
lowercase, I'd get intellisense and API checking. For this view, we are also using a master page.
Listing 1.11 Passing in the presentation model to the RenderView method
using System.Web.Mvc;
namespace MvcApplication.Controllers
{
public class HelloWorld4Controller : Controller
{
public ActionResult Index()
{
//casting to an object to ensure correct
//overload is called
return View((object)"Hello World4");
}
}
}
Listing 1.12 Strongly typed view with master page.
<%@ Page Inherits="System.Web.Mvc.ViewPage<string>"
MasterPageFile="~/Views/Shared/HelloWorld.Master"%>
<%@ Import Namespace="MvcApplication.Controllers"%>
<asp:Content ID="Content2" ContentPlaceHolderID="Main" runat="server">
<h1><%=Model %>! I'm strongly typed in a layout!</h1>
</asp:Content>
Notice that we have a
Model
property available that is the type declared in the Inherits tag. In listing 1.12 we
have chosen
System.Web.Mvc.ViewPage<T>
. The generic base class allows us to strongly-type the
ViewData
property because the
ViewData
property is of type
ViewDataDictionary<T>
, and the
Model
property is of
type
T.
In this example, the view has chosen the master page, but you can also choose the master inside the controller
action. It is up to you, but not all view engines support the view specifying the master. Master pages function
exactly the same as in Web Forms for templating, but the server side postback and
ViewState
mechanism are
irrelevant. Rendering is the only responsibility of the view and the master page. Listing 1.13 shows our master
page, which outlines the structure for the page. The layout declares
System.Web.MVC.ViewMasterPage
as the
base type.
Listing 1.13 The layout for our view. The purpose is to provide the layout structure for the view. Be sure not
to put any behavior here.
<%@ Master Language="C#" AutoEventWireup="true"
Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >