Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the location of the default Editor and Display Templates in Asp.net MVC3?

Where are the default Razor Editor and Display templates (eg String.cshtml, DateTime.cshtml) located when one installs Asp.Net MVC 3?

like image 296
Ahmad Avatar asked Oct 17 '11 09:10

Ahmad


People also ask

What are display and editor templates in ASP NET MVC?

ASP.NET MVC has made this kind of standardization easy to do with the inclusion of display and editor templates. Let's walk through these features. In short, display and editor templates are used to standardize the layout shown to the user when editing or displaying certain types or classes.

How do you display a template in MVC?

Display templates. MVC has a bunch of handy helpers that we can use to create our views more efficiently. One such helper are the display templates that are used within views. The DisplayFor(Func<TModel, TValue> expression) function uses the type of the property in the expression to display the property value.

How to override default MVC display templates?

We can override the default templates by placing our custom display templates into the path Views/Shared/DisplayTemplates/<type>.cshtml. They are structured like any MVC partial view.

How do I use editorfor in MVC?

Note the use of EditorFor on this template. If we were to use EditorFor on a primitive type, MVC will simply display the bext <input> for that type (usually it'll be a text box). In short, use editor templates when you need to standardize how the layout of a class should look when editing that class.


2 Answers

If you have DotPeek or Reflector you can look up the type DefaultDisplayTemplates in there you will find the templates. But be warned they are in Code format, not WebForm or razor format, so a bit more difficult to interpret.

StringTemplate

internal static string StringTemplate(HtmlHelper html)
{
  return html.Encode(html.ViewContext.ViewData.TemplateInfo.FormattedModelValue);
}

(There was no default DateTime template that I could find)

DecimalTemplate

internal static string DecimalTemplate(HtmlHelper html)
{
  if (html.ViewContext.ViewData.TemplateInfo.FormattedModelValue == html.ViewContext.ViewData.ModelMetadata.Model)
    html.ViewContext.ViewData.TemplateInfo.FormattedModelValue = (object) string.Format((IFormatProvider) CultureInfo.CurrentCulture, "{0:0.00}", new object[1]
    {
      html.ViewContext.ViewData.ModelMetadata.Model
    });
  return DefaultDisplayTemplates.StringTemplate(html);
}
like image 102
Paul Tyng Avatar answered Oct 16 '22 01:10

Paul Tyng


There are no default templates.Razor Editor and Display methods are extension methods of the class HtmlHelper. You can use them or you can develope your own extension methods, like this example.

public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
        {
            MvcHtmlString result = InputExtensions.TextBoxFor(helper, expression);
            // do modification to result
            return result;
        }
like image 43
Massimo Zerbini Avatar answered Oct 16 '22 02:10

Massimo Zerbini