ASP.net MVC provides two different options to create re-usable components
- Partial Views
- Display/Editor Templates
But whats the different between both of them and what advantage one has got over the other.
Difference between Display and Editor Templates:
Display templates and Editor templates are model driven templates but only differ by the convention that display should only render read only html like divs, labels or spans etc where are editor should render editable html with forms, input controls, etc. Developers are the one whole create these templates and there is no validation which prevent us to create templates in the opposite order. It is the convention over configuration policy which drives MVC to follow a specific standardize pattern and sticking to this will ensure that no matter what you pass into Display helper method(s) will always render the same result as expected. Once we specify UIHint on any property, we instructor MVC view engine to render the respective property using given template specified either under DisplayTemplates folder or EditorTemplates folder under shared folder depending whether we want readonly mode (Html.Display) or editable mode (Html.Editor).
Sample ViewModel for the same to demonstrate how we ViewModel can be decorated with attributes to leverage templates rendering thru ViewModels in MVC
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using ReferenceImplementation.ViewModels; namespace ReferenceImplementation.ViewModels { public class PersonViewModel { public string FirstName { get; set; } public string LastName { get; set; } [UIHint("Int")] public int Age { get; set; } [UIHint("Date")] public DatetimeViewModel DateOfBirth { get; set; } [UIHint("Phone")] public PhoneViewModel MobileNumber { get; set; } } }
By specifying the UIHint attribute on properties in ViewModel, we are instructing MVC to render these properties with the respective templates (depending on its display or editor reference) from the respective folder instead of using standard MVC editor.
For the view code like the one given below
<div class="readonly">@Html.DisplayFor(m => m.DateOfBirth)</div>
Since we are using Display helper method, MVC will look for the Date template under DisplayTemplates folder and for the view code given below
<div class="editale">@Html.EditorFor(m => m.Phone)</div>
MVC will look for template under EditorTemplates folder for the Editor helper method.
Difference between Partial View and Display/Editor Templates
By convention, Partial Views are considered to be View Centeric and MVC Templates are considered to be Model Centeric. This means that templates are more dependent on the view model and way they are rendered depends a lot on their properties but same is not true for the partial view as you are more concerned in choosing the correct partial view.
Partial View differs from Templates in the way they render Id's from the ViewModels. Partial view render the element name as it is but MVC Templates adhere to model hierarchies when rendering HTML helpers. e.g if you have a "Bar" object on your "Foo" model, the HTML elements for "Bar" will be rendered with "Foo.Bar.ElementName", whilst a partial will have "ElementName".
MVC Templates are more robust and smart. If you had a List<T> of something in your ViewModel, you could use @Html.DisplayFor(m => m.CollectionOfFoo), MVC templates are smart enough to see it as a collection and render out the single display for each item as opposed to a Partial, which would require an explicit for loop.
Templates are passed with additional information that partial views are not, in particular you receive ModelMetadata, such as that created by attributes. ModelMetadata are part of ViewData which is also accessible in partial view but are null as its only populated in templates ().
Note: Templates are partials which adhere to a specific convention. The situations which make templates better or worse than old partials are almost strictly dependent on whether or not the convention is worth adherence in your application


