Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

c# - Cannot find DisplayTemplates for common datatypes

I was following this YouTube tutorial for MVC and I had some doubts regarding the EditorFor and LabelFor HTMLHelpers and looked it up online. Found this blog which explains in detail. However, in the blog, the mentions that we need to have the [datatype].cshtml file in our ViewsShared folder for us to be able to apply the EditorFor/LabelFor methods to the properties of datatype [datatype]. But in my project, I don't see any String.cshtml file anywhere in the solution. So how come I am able to use the EditorFor method for AccountNumber property which is of datatype String. Sample code below

Shouldn't I be having the String.cshtml file in ViewsShared folder to use these EditorFor methods?

 <div class="form-group">
        @Html.LabelFor(model => model.AccountNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.AccountNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.AccountNumber, "", new { @class = "text-danger" })
        </div>
    </div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Stephen Muecke mentioned in the comments, there are default templates in the System.Web.Mvc.Html namespace. I think the comment really says it all, but I'll try to clarify what's going on behind the scenes. If you check the source code for EditorFor you will eventually end up at the internal static TemplateHelpers, which contain dictionaries for some default types, which in turn use DefaultEditorTemplates. These are used until you actually override them yourself by placing a view inside the folders Shared/EditorTemplates or Shared/DisplayTemplates. So, even if Boolean is in the dictionary of the default templates, you may override it by placing Boolean.cshtml in the on of the folders.

From TemplateHelpers:

private static readonly Dictionary<string, Func<HtmlHelper, string>> _defaultEditorActions = new Dictionary<string, Func<HtmlHelper, string>>((IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase) {
  { "HiddenInput", new Func<HtmlHelper, string>(DefaultEditorTemplates.HiddenInputTemplate) },
  { "MultilineText", new Func<HtmlHelper, string>(DefaultEditorTemplates.MultilineTextTemplate) },
  { "Password", new Func<HtmlHelper, string>(DefaultEditorTemplates.PasswordTemplate) },
  { "Text", new Func<HtmlHelper, string>(DefaultEditorTemplates.StringTemplate) },
  { "Collection", new Func<HtmlHelper, string>(DefaultEditorTemplates.CollectionTemplate) },
  { "PhoneNumber", new Func<HtmlHelper, string>(DefaultEditorTemplates.PhoneNumberInputTemplate) },
  { "Url", new Func<HtmlHelper, string>(DefaultEditorTemplates.UrlInputTemplate) },
  { "EmailAddress", new Func<HtmlHelper, string>(DefaultEditorTemplates.EmailAddressInputTemplate) },
  { "DateTime", new Func<HtmlHelper, string>(DefaultEditorTemplates.DateTimeInputTemplate) },
  { "DateTime-local", new Func<HtmlHelper, string>(DefaultEditorTemplates.DateTimeLocalInputTemplate) },
  { "Date", new Func<HtmlHelper, string>(DefaultEditorTemplates.DateInputTemplate) },
  { "Time", new Func<HtmlHelper, string>(DefaultEditorTemplates.TimeInputTemplate) },
  { typeof (Color).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.ColorInputTemplate) },
  { typeof (byte).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.NumberInputTemplate) },
  { typeof (sbyte).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.NumberInputTemplate) },
  { typeof (int).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.NumberInputTemplate) },
  { typeof (uint).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.NumberInputTemplate) },
  { typeof (long).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.NumberInputTemplate) },
  { typeof (ulong).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.NumberInputTemplate) },
  { typeof (bool).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.BooleanTemplate) },
  { typeof (Decimal).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.DecimalTemplate) },
  { typeof (string).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.StringTemplate) },
  { typeof (object).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.ObjectTemplate) }
};

From DefaultEditorTemplates for creating a Boolean:

internal static string BooleanTemplate(HtmlHelper html)
{
  bool? nullable1 = new bool?();
  if (html.ViewContext.ViewData.Model != null)
    nullable1 = new bool?(Convert.ToBoolean(html.ViewContext.ViewData.Model, (IFormatProvider) CultureInfo.InvariantCulture));
  if (html.ViewContext.ViewData.ModelMetadata.IsNullableValueType)
    return DefaultEditorTemplates.BooleanTemplateDropDownList(html, nullable1);
  HtmlHelper html1 = html;
  bool? nullable2 = nullable1;
  int num = nullable2.HasValue ? (nullable2.GetValueOrDefault() ? 1 : 0) : 0;
  return DefaultEditorTemplates.BooleanTemplateCheckbox(html1, num != 0);
}

private static string BooleanTemplateCheckbox(HtmlHelper html, bool value)
{
  return InputExtensions.CheckBox(html, string.Empty, value, DefaultEditorTemplates.CreateHtmlAttributes(html, "check-box", (string) null)).ToHtmlString();
}

private static string BooleanTemplateDropDownList(HtmlHelper html, bool? value)
{
  return SelectExtensions.DropDownList(html, string.Empty, (IEnumerable<SelectListItem>) DefaultEditorTemplates.TriStateValues(value), DefaultEditorTemplates.CreateHtmlAttributes(html, "list-box tri-state", (string) null)).ToHtmlString();
}

Here's the source code from Core:

https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/TemplateRenderer.cs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...