Where are the default Razor Editor and Display templates (eg String.cshtml, DateTime.cshtml) located when one installs Asp.Net MVC 3?
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.
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.
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.
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.
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);
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With