Skip to Main Content
ASP.NET 4 24-Hour Trainer
book

ASP.NET 4 24-Hour Trainer

by Toi B. Wright
July 2010
Beginner content levelBeginner
552 pages
10h 14m
English
Wrox
Content preview from ASP.NET 4 24-Hour Trainer

Chapter 21B. Display Templates in MVC

Display templates are templated helpers that enable you to build a user interface based on your data model. In this lesson I show you how to use display templates to display the data in your model.

Here are the three helper methods in the DisplayExtensions class:

  • Display() — This helper returns HTML markup for a particular property in the model where the property is identified via a string expression.

  • DisplayFor() — This helper also returns HTML markup for a particular property in the model. However, with this helper, the property is identified via a strongly-typed expression.

  • DisplayForModel() — This helper returns HTML markup for all of the simple properties in the model.

    Note

    The expression DisplayFor(model => model) is equivalent to DisplayForModel().

THE DISPLAYFORMODEL HELPER

Figure 21B-1 shows the Entity Data Model (EDM) that I use in this lesson.

FIGURE 21B-1

Figure 21B.1. FIGURE 21B-1

This is the Details action method:

//
// GET: /Task/Details/5

public ActionResult Details(int id = 1)
{
    var task = (from t in _entity.Tasks
                where t.Id == id
                select t).FirstOrDefault();

    return View(task);
}

In the preceding action method I have set the default value of the id parameter to 1. This is the View that uses the DisplayForModel helper:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Lesson21b.Models.Task>" %> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ASP.NET 4 24-Hour Trainer

ASP.NET 4 24-Hour Trainer

Toi B. Wright
ASP.NET 4 Unleashed

ASP.NET 4 Unleashed

Stephen Walther, Kevin Hoffman, Nate Dudek

Publisher Resources

ISBN: 9780470596913Purchase bookExamplesErrata