Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find the default Object.cshtml Editor template?

I need to modify the default editor template for scaffolding but I havent found the Object.cshtml template, where can I find the default razor Object.cshtml Editor template?

like image 550
ryudice Avatar asked Feb 11 '11 17:02

ryudice


People also ask

What is MVC editor template?

An EditorTemplate is a Razor file placed in the EditorTemplates folder: For Razor Pages apps, in the Pages/Shared/EditorTemplates folder. For MVC apps, in the Views/Shared/EditorTemplates folder or the Views/ControllerName/EditorTemplates folder.

Is Cshtml HTML5?

You write HTML5 like simple HTML in the views (. cshtml). As for the toolkits I recommend finding which control you like to use and searching for an HTML5 library that has it. You can find almost anything if you know what you are looking for.


2 Answers

The following blog post describes how to customize the editor templates: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

Basically you have to add a file named Views\Shared\EditorTemplates\Object.cshtml and put all the logic for displaying the object there.

like image 64
marcind Avatar answered Oct 19 '22 03:10

marcind


When @marcind says they're compiled in, the templates themselves are not embedded but are rather written in code. For example, EditorFor calls TemplateFor which may call TextAreaExtensions.TextArea or one of many other extensions which generate the code that is ultimately output. This may be because the we have the option of removing the default view engine and using something like nhaml.

The mapping between the template names and the function creating the resulting output can be seen in System.Web.Mvc.Html.TemplateHelpers. See also System.Web.Mvc.Html.DefaultEditorTemplates.

The closest thing that exists right now are the Webforms templates that exist in Mvc3Futures which are available on the aspnet.codeplex.com website. Within it exists an DefaultTemplates\EditorTemplates folder that contains the templates.

Here's the Object.ascx template:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    bool ShouldShow(ModelMetadata metadata) {
        return metadata.ShowForEdit
            && metadata.ModelType != typeof(System.Data.EntityState)
            && !metadata.IsComplexType
            && !ViewData.TemplateInfo.Visited(metadata);
    }
</script>
<% if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
    <% if (Model == null) { %>
        <%= ViewData.ModelMetadata.NullDisplayText %>
    <% } else { %>
        <%= ViewData.ModelMetadata.SimpleDisplayText %>
    <% } %>
<% } else { %>    
    <% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => ShouldShow(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>
        <% } %>
    <% } %>
<% } %>
like image 28
Kaleb Pederson Avatar answered Oct 19 '22 03:10

Kaleb Pederson