Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3: Where can I find default templates for DisplayForModel and EditorForModel?

Where can I find the default templates for DisplayForModel and EditorForModel?

like image 771
Bridget the Midget Avatar asked Dec 06 '11 07:12

Bridget the Midget


2 Answers

Found the default templates here:

ASP.NET MVC 3 Futures / http://aspnet.codeplex.com/releases/view/58781

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    bool ShouldShow(ModelMetadata metadata) {
        return metadata.ShowForDisplay
            && metadata.ModelType != typeof(System.Data.EntityState)
            && !metadata.IsComplexType
            && !ViewData.TemplateInfo.Visited(metadata);
    }
</script>
<% 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 => ShouldShow(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>
        <% } %>
    <% } %>
<% } %>
like image 138
Bridget the Midget Avatar answered Oct 13 '22 11:10

Bridget the Midget


I'm not sure if/where you can see the defaults without looking at the source code but you can create your own in the following directories to override the defaults:

~/Views/Shared/EditorTemplates/Object.cshtml

and

~/Views/Shared/DisplayTemplates/Object.cshtml

Does this help?

Here's a great post on Brad Wilson's blog that will walk you through how to create your own default editor and display templates: ASP.NET MVC 2 Templates, Part 4: Custom Object Templates

like image 29
Jeff Camera Avatar answered Oct 13 '22 11:10

Jeff Camera