Introduction to Templates
Html.Display
- String based: <%= Html.Display(“PropertyName”) %>
- Expression based: <%= Html.DisplayFor(model => model.PropertyName) %>
- Model: <%= Html.DisplayForModel() %>
1
2
3
4
5
| public class Contact { public string FirstName { get ; set ; } public string LastName { get ; set ; } public int Age { get ; set ; } } |
1
2
3
| public ViewResult Details([DefaultValue(0)] int id) { return View(contacts[id]); } |
1
2
3
4
5
6
7
| <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage" %> < asp:Content ContentPlaceHolderID = "MainContent" runat = "server" > <%= Html.DisplayForModel() %> </ asp:Content > |
Html.Editor
- String based: <%= Html.Editor(“PropertyName”) %>
- Expression based: <%= Html.EditorFor(model => model.PropertyName) %>
- Model: <%= Html.EditorForModel() %>
What’s Really Happening?
1
2
3
4
| <% foreach (var prop in ViewData.ModelMetadata.Properties) { %> < div class = "display-label" ><%= prop.GetDisplayName() %></ div > < div class = "display-field" ><%= Html.Display(prop.PropertyName) %></ div > <% } %> |
1
| <%= Html.Encode(Model) %> |
1
2
| <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <%= Html.Encode(Model) %> < em >Hello there!</ em > |
Wrapping Up
No comments:
Post a Comment