ASP.NET MVC 2 has provided a lot more options for displaying the data to the end user within your views. Here are a few of the options you have:
Html.DisplayFor(i => i.FirstName) – DisplayFor renders the value of a property directly to the browser. In this example, the FirstName property returns a string, which gets rendered. What is “i” in this instance? This is a reference to the Model that’s bound to the view in the @Page definition. Remember that the Model is accessible as a part of the ViewDataDictionary class (accessible via ViewData property of the controller), and that views either store an object reference to the model, or a generic reference. Using some of these new syntaxes was built to be more convenient with the generic forms (the view inheriting from ViewPage<T> rather than ViewPage).
<%= Html.DisplayFor(i => i.FirstName).ToHtmlString() %>
<%= Html.DisplayFor(i => i.LastName).ToHtmlString() %>
DisplayFor can display a simple property within this model if you like, or it has the ability to display more complex types, as we’ll see more details about this soon.
Html.DisplayForModel() – Renders a display view for the entire model object; by default, renders a label and display element for each property in the object. It’s possible to completely customize the UI for this, using the new template feature in ASP.NET MVC 2 (more on this later too).
<%= Html.DisplayForModel() %>
Html.DisplayText() – Displays a property value within the model referencing the value via a string; it take the name of a property or expression to display data, and renders that data as text.
<%= Html.DisplayText(“Title”) %>
Interesting thing about templates; the latest version of MVC includes the templating features similar to what you see in Dynamic Data. You can create, in the controller or shared folder, displaytemplates and editortemplates folders that contains the templates for the display or edit versions of the templates for the data being displayed. By default, simple properties get displayed using in-built templates created by Microsoft. So, when the code to display the first name (shown above) executes, the string value being shown uses a built-in template to render the content. Brad Wilson covered this in a blog post in great detail: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html.
In these special folders, you can create custom templates for simple data types (int, float, decimal, string, etc.), or create templates for complex objects (business objects and such). For instance, suppose you have a Contact class generated from a LINQ to SQL model. We can create a specialized UI for displaying the data in a folder structure shown below.
Contact.ascx (partial view)
Anything displaying a Contact LINQ to SQL object will use this contact UI form, which has the following definition:
<%@ Control Language=”C#”
Custom Contact Template
<br /><br />
<%= Model.FirstName %>
<%= Model.LastName %>
So how does the DisplayFor method know which template to use? The framework uses the template similar to the following:
<%= Html.DisplayFor(i => i.Contact, “ContactTemplate”); //ContactTemplate matches the user control name
By default, it will do a name match of the property to the template name, but you can also customize the process by specifying the name manually as illustrated above. This explicit reference searches for the template within either the ~/Views/<Controller>/DisplayTemplates/ContactTemplate.ascx or the ~/Views/Shared/DisplayTemplates/ContactTemplate.ascx file. Now, we have one centralized template for the Contact type, reusable across the entire application.