找不到常见数据类型的DisplayTemplates [英] Cannot find DisplayTemplates for common datatypes

查看:57
本文介绍了找不到常见数据类型的DisplayTemplates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 YouTube MVC教程,我对EditorFor和LabelFor有一些疑问HTMLHelpers并在线查找它.找到博客,该博客进行了详细说明.但是,在博客中,提到我们需要在Views \ Shared文件夹中具有[datatype] .cshtml文件,才能将EditorFor/LabelFor方法应用于数据类型[datatype]的属性.但是在我的项目中,解决方案中的任何地方都看不到任何String.cshtml文件.因此,我怎么能对数据类型为String的AccountNumber属性使用EditorFor方法.下面的示例代码

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 Views\Shared 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

我应该在Views \ Shared文件夹中没有String.cshtml文件来使用这些EditorFor方法吗?

Shouldn't I be having the String.cshtml file in Views\Shared 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>

推荐答案

评论中提到的 Stephen Muecke System.Web.Mvc.Html 命名空间中有默认模板.我认为评论确实说明了一切,但我将尽力弄清幕后发生的事情.如果您检查 EditorFor ,您最终会遇到内部静态 TemplateHelpers ,其中包含某些默认类型的字典,依次使用 DefaultEditorTemplates .除非您实际将它们放置在文件夹 Shared/EditorTemplates Shared/DisplayTemplates 内,然后使用它们,否则您将一直使用它们.因此,即使 Boolean 在默认模板的词典中,您也可以通过将 Boolean.cshtml 放在文件夹的上方来覆盖它.

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.

来自 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) }
};

DefaultEditorTemplates 中创建 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();
}

以下是Core的源代码:

Here's the source code from Core:

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

这篇关于找不到常见数据类型的DisplayTemplates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆