Display And Edit Templated Helpers In ASP.NET MVC

In this tutorial we will discuss Template helper in ASP.NET MVC. Template helper is introduced in MVC-2, and it is classified into two categories.
  1. Display template
  2. Editor Template
Display Template Helper Type
  1. @Html.DisplayForModel() :- This Display template helper is used with strongly typed views. It goes through the every property in the model for displaying object.

  2. @Html.DisplayFor(modal => modal) :- This Display template helper is used with strongly typed views , if model has properties which return complex objects then this template helper is very useful.

  3. @Html.Display(“StudentData”) :- This Display template helper is not a strongly typed view. If we store data in viewData then this template helper is used to get that data by key.
Editor Template Helper Type

Like Display template Editor template also has three types,
  1. @Html.EditorForModel()
  2. @Html.Editor("StudentData")
  3. @Html.EditorFor(model => model)
These helpes also work like display template.

@Html.DisplayForModel() And @Html.EditorForModel()

For understanding this template we will take previous example class which we have designed like following. Previous example
link is,
  1. [MetadataType(typeof(StudentMetaData))]  
  2. public partial class Student  
  3. {  
  4. }  
  5. public class StudentMetaData  
  6. {  
  7. #region dataType attribute  
  8. [HiddenInput(DisplayValue = false)]  
  9. public int Id { getset; }  
  10. [ReadOnly(true)]  
  11. [DataType(DataType.EmailAddress)]  
  12. public string EmailAddress { getset; }  
  13. [DataType(DataType.Currency)]  
  14. public int? WinningPrize { getset; }  
  15. // Generate a hyperlink  
  16. [DataType(DataType.Url)]  
  17. [UIHint("OpenInNewWindow")]  
  18. public string PersonalWebSite { getset; }  
  19. [DataType(DataType.Date)]  
  20. public DateTime? AdmissionDate { getset; }  
  21. [DisplayAttribute(Name = "Full Name")]  
  22. public string FullName { getset; }  
  23.   
  24. [DisplayFormat(DataFormatString = "{0:d}")]  
  25. public DateTime? HireDate { getset; }  
  26.   
  27. [DisplayFormat(NullDisplayText = "Gender not specified")]  
  28. public string Gender { getset; }  
  29. #endregion  
  30. }  
Now create a Home Controller and create Action method for Detail and Edit, we are taking previous example there we have already designed these action method.

The Detail action method code is like following,
  1. public ActionResult Details(int id)  
  2. {  
  3. MVCVariousAttributeEntities _context = new MVCVariousAttributeEntities();  
  4. Student _studentDetail = _context.Students.Where(c => c.Id == id).SingleOrDefault();  
  5. return View(_studentDetail);  
  6. }  
Create a view for Detail Action method with the strongly typed view and Scaffold template as “Detail”. 
When you add this view then you will see lot of HTML code in this DetailView like following.
  1. @model MVCApplicationWithVariousAttribute.Models.Student  
  2. @{  
  3. ViewBag.Title = "Details";  
  4. }  
  5. <div style = >  
  6. <h2>Details</h2>  
  7. <fieldset>  
  8. <legend>Student</legend>  
  9. <div class="display-label">  
  10. @Html.DisplayNameFor(model => model.FullName)  
  11. </div>  
  12. <div class="display-field">  
  13. @Html.DisplayFor(model => model.FullName)  
  14. </div>  
  15. <div class="display-label">  
  16. @Html.DisplayNameFor(model => model.Gender)  
  17. </div>  
  18. <div class="display-field">  
  19. @Html.DisplayFor(model => model.Gender)  
  20. </div>  
  21. <div class="display-label">  
  22. @Html.DisplayNameFor(model => model.EmailAddress)  
  23. </div>  
  24. <div class="display-field">  
  25. @Html.DisplayFor(model => model.EmailAddress)  
  26. </div>  
  27. <div class="display-label">  
  28. @Html.DisplayNameFor(model => model.PersonalWebSite)  
  29. </div>  
  30. <div class="display-field">  
  31. @Html.DisplayFor(model => model.PersonalWebSite)  
  32. </div>  
  33. </fieldset>  
  34. </div>  
Instead of this we can use one the Display template so the view code will be like,
  1. @model MVCApplicationWithVariousAttribute.Models.Student  
  2. @{  
  3. ViewBag.Title = "Details";  
  4. }  
  5. <div style = >  
  6. <h2>Details</h2>  
  7. @Html.DisplayForModel()  
  8. For the edit action method we can do same  
  9. @model MVCApplicationWithVariousAttribute.Models.Student  
  10. @{  
  11. ViewBag.Title = "Edit";  
  12. }  
  13. <h2>Edit</h2>  
  14. <div style= Arial>  
  15. @using (@Html.BeginForm())  
  16. {  
  17. @Html.EditorForModel()  
  18. }  
  19. </div>  
@Html.Display(“StudentData”) And @Html.Editor(“StudentData”)

This template is used when do not use scaffold template and we pass data through ViewData or ViewBeg. 
Code for Edit Action method will be like following,
  1. public ActionResult Details(int id)  
  2. {  
  3. MVCVariousAttributeEntities _context = new MVCVariousAttributeEntities();  
  4. Student _studentDetail = _context.Students.Where(c => c.Id == id).SingleOrDefault();  
  5. ViewData["StudentData"] = _studentDetail;  
  6. return View();  
  7. return View(_studentDetail);  
  8. }  
And the view code will be like following,
  1. @{  
  2. ViewBag.Title = "Details";  
  3. }  
  4.   
  5. <h2>Details</h2>  
  6.   
  7. <fieldset>  
  8. <legend>Student</legend>  
  9. @Html.Display("StudentData")  
  10. </fieldset>  
We can do the same with Edit template also, s
o when we run this application in all the conditions, output will be the same for the display template output.

The edit template will be,




you can go to my blog also.