生成的文本字段而不是数字

时间:2016-12-17 22:09:20

标签: c# html asp.net-core

给定具有定义为

的属性的模型
[Display(Name = nameof(Amount), ResourceType = typeof(Resources.ViewModels.MyViewModel))]
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Generic))]
[Range(0.1, double.MaxValue, ErrorMessageResourceName = "AboveZero", ErrorMessageResourceType = typeof(Resources.Generic))]
public decimal Amount { get; set; }

在视图中使用:

<input id="amount" asp-for="Amount" class="form-control" />

生成<input type="text" ...>

如果我添加type="number",则会正确生成一个数字字段。这是一个错误或小数被区别对待,如果是这样,为什么?

1 个答案:

答案 0 :(得分:1)

这不是错误。 Asp.net Core将十进制类型映射到文本字段: 见InputTagHelper.cs

 private static readonly Dictionary<string, string> _defaultInputTypes =
        new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        {
            { "HiddenInput", InputType.Hidden.ToString().ToLowerInvariant() },
            { "Password", InputType.Password.ToString().ToLowerInvariant() },
            { "Text", InputType.Text.ToString().ToLowerInvariant() },
            { "PhoneNumber", "tel" },
            { "Url", "url" },
            { "EmailAddress", "email" },
            { "Date", "date" },
            { "DateTime", "datetime" },
            { "DateTime-local", "datetime-local" },
            { "Time", "time" },
            { nameof(Byte), "number" },
            { nameof(SByte), "number" },
            { nameof(Int16), "number" },
            { nameof(UInt16), "number" },
            { nameof(Int32), "number" },
            { nameof(UInt32), "number" },
            { nameof(Int64), "number" },
            { nameof(UInt64), "number" },
            { nameof(Single), InputType.Text.ToString().ToLowerInvariant() },
            { nameof(Double), InputType.Text.ToString().ToLowerInvariant() },
            { nameof(Boolean), InputType.CheckBox.ToString().ToLowerInvariant() },
            { nameof(Decimal), InputType.Text.ToString().ToLowerInvariant() },
            { nameof(String), InputType.Text.ToString().ToLowerInvariant() },
            { nameof(IFormFile), "file" },
            { TemplateRenderer.IEnumerableOfIFormFileName, "file" },
        };