On All Things Web

Mar 18

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.


Views
   Shared
       DisplayTemplates
          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#”
 Inherits=”System.Web.Mvc.ViewUserControl<ASPMVC2.DataAccess.DataModels.Contact>” %>


<strong>
Custom Contact Template
</strong>


<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.

2 comments so far

  1. ffffffffffffffffffffffffffffffffffffffffffffffffffffff'''
    2:57 am - 7-28-2010

    wefef

  2. asp net file picker
    1:55 pm - 12-5-2010

    FileUltimate is an ASP.NET file manager control which you can add directly to your existing ASP.NET (.aspx) pages. The control renders a user interface similar to “Windows Explorer” within the page and this user interface allows you to view the contents of the predefined root folders and allow complete file management actions within those folders. File management actions can be limited by predefined permissions and quota limits on each folder separately.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>