Tuesday, June 16, 2015

Template Resolution in MVC


Template Resolution
  • ~/Areas/AreaName/Views/ControllerName/DisplayTemplates/TemplateName.aspx & .ascx
  • ~/Areas/AreaName/Views/Shared/DisplayTemplates/TemplateName.aspx & .ascx
  • ~/Views/ControllerName/DisplayTemplates/TemplateName.aspx & .ascx
  • ~/Views/Shared/DisplayTemplates/TemplateName.aspx & .ascx
  • TemplateHint from ModelMetadata
  • DataTypeName from ModelMetadata
  • The name of the type (see notes below)
  • If the object is not complex: “String”
  • If the object is complex and an interface: “Object”
  • If the object is complex and not an interface: Recurse through the inheritance hiearchy for the type, trying every type name
The TemplateInfo Class
Built-In Display Templates?
1
2
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>
?
1
2
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= ViewData.TemplateInfo.FormattedModelValue %>
?
1
2
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<a href="mailto:<%= Html.AttributeEncode(Model) %>"><%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %></a>
?
1
2
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<a href="<%= Html.AttributeEncode(Model) %>"><%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %></a>
?
1
2
3
4
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% if (!ViewData.ModelMetadata.HideSurroundingHtml) { %>
    <%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>
<% } %>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private object FormattedValue {
        get {
            if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) {
                return String.Format(System.Globalization.CultureInfo.CurrentCulture,
                                     "{0:0.00}",
                                     ViewData.ModelMetadata.Model);
            }
            return ViewData.TemplateInfo.FormattedModelValue;
        }
    }
</script>
<%= Html.Encode(FormattedValue) %>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private bool? ModelValue {
        get {
            bool? value = null;
            if (ViewData.Model != null) {
                value = Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
            }
            return value;
        }
    }
</script>
<% if (ViewData.ModelMetadata.IsNullableValueType) { %>
    <select class="list-box tri-state" disabled="disabled">
        <option value="" <%= ModelValue.HasValue ? "" : "selected='selected'" %>>Not Set</option>
        <option value="true" <%= ModelValue.HasValue && ModelValue.Value ? "selected='selected'" : "" %>>True</option>
        <option value="false" <%= ModelValue.HasValue && !ModelValue.Value ? "selected='selected'" : "" %>>False</option>
    </select>
<% } else { %>
    <input class="check-box" disabled="disabled" type="checkbox" <%= ModelValue.Value ? "checked='checked'" : "" %> />
<% } %>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% if (Model == null) { %>
    <%= ViewData.ModelMetadata.NullDisplayText %>
<% }
   else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
    <%= ViewData.ModelMetadata.SimpleDisplayText %>
<% }
   else { %>
    <% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay
                         && !ViewData.TemplateInfo.Visited(pm))) { %>
        <% if (prop.HideSurroundingHtml) { %>
            <%= Html.Display(prop.PropertyName) %>
        <% }
           else { %>
            <% if (!String.IsNullOrEmpty(prop.GetDisplayName())) { %>
                <div class="display-label"><%= prop.GetDisplayName() %></div>
            <% } %>
            <div class="display-field"><%= Html.Display(prop.PropertyName) %></div>
        <% } %>
    <% } %>
<% } %>
?
1
2
3
<% if (Model == null) { %>
    <%= ViewData.ModelMetadata.NullDisplayText %>
<% }
?
1
2
3
   else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
    <%= ViewData.ModelMetadata.SimpleDisplayText %>
<% }
?
1
2
3
else { %>
 <% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay
                      && !ViewData.TemplateInfo.Visited(pm)))
?
1
2
3
<% if (prop.HideSurroundingHtml) { %>
    <%= Html.Display(prop.PropertyName) %>
<% }
?
1
2
3
4
<% if (!String.IsNullOrEmpty(prop.GetDisplayName())) { %>
    <div class="display-label"><%= prop.GetDisplayName() %></div>
<% } %>
<div class="display-field"><%= Html.Display(prop.PropertyName) %></div>
Built-In Editor Templates?
1
2
3
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
                 new { @class = "text-box single-line" }) %>
?
1
2
3
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Password("", ViewData.TemplateInfo.FormattedModelValue,
                  new { @class = "text-box single-line password" }) %>
?
1
2
3
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.TextArea("", ViewData.TemplateInfo.FormattedModelValue.ToString(),
                  0, 0, new { @class = "text-box multi-line" }) %>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private object ModelValue {
        get {
            if (Model is System.Data.Linq.Binary) {
                return Convert.ToBase64String(((System.Data.Linq.Binary)Model).ToArray());
            }
            if (Model is byte[]) {
                return Convert.ToBase64String((byte[])Model);
            }
            return Model;
        }
    }
</script>
<% if (!ViewData.ModelMetadata.HideSurroundingHtml) { %>
    <%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>
<% } %>
<%= Html.Hidden("", ModelValue) %>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private object ModelValue {
        get {
            if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) {
                return String.Format(System.Globalization.CultureInfo.CurrentCulture,
                                     "{0:0.00}", ViewData.ModelMetadata.Model);
            }
            return ViewData.TemplateInfo.FormattedModelValue;
        }
    }
</script>
<%= Html.TextBox("", ModelValue, new { @class = "text-box single-line" }) %>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private List<SelectListItem> TriStateValues {
        get {
            return new List<SelectListItem> {
                new SelectListItem { Text = "Not Set",
                                     Value = String.Empty,
                                     Selected = !Value.HasValue },
                new SelectListItem { Text = "True",
                                     Value = "true",
                                     Selected = Value.HasValue && Value.Value },
                new SelectListItem { Text = "False",
                                     Value = "false",
                                     Selected = Value.HasValue && !Value.Value },
            };
        }
    }
    private bool? Value {
        get {
            bool? value = null;
            if (ViewData.Model != null) {
                value = Convert.ToBoolean(ViewData.Model,
                                          System.Globalization.CultureInfo.InvariantCulture);
            }
            return value;
        }
    }
</script>
<% if (ViewData.ModelMetadata.IsNullableValueType) { %>
    <%= Html.DropDownList("", TriStateValues, new { @class = "list-box tri-state" })%>
<% } else { %>
    <%= Html.CheckBox("", Value ?? false, new { @class = "check-box" })%>
<% } %>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
    <%= ViewData.ModelMetadata.SimpleDisplayText%>
<% }
   else { %>   
    <% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit
                         && !ViewData.TemplateInfo.Visited(pm))) { %>
        <% if (prop.HideSurroundingHtml) { %>
            <%= Html.Editor(prop.PropertyName) %>
        <% }
           else { %>
            <% if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString())) { %>
                <div class="editor-label"><%= Html.Label(prop.PropertyName) %></div>
            <% } %>
            <div class="editor-field">
                <%= Html.Editor(prop.PropertyName) %>
                <%= Html.ValidationMessage(prop.PropertyName, "*") %>
            </div>
        <% } %>
    <% } %>
<% } %>

No comments:

Post a Comment