【问题标题】:How to make entry field to allow numbers only using EF and Data Annotations?如何使输入字段仅允许使用 EF 和数据注释的数字?
【发布时间】:2012-12-21 05:10:22
【问题描述】:

我试图弄清楚是否有办法确保使用数据注释和实体框架仅允许数字输入。

我正在使用以下代码

[Required]
[DisplayName("Client No")]
[Column("client_no", TypeName = "smallint")]
public virtual Int16 Number { get; set; }

我希望使用数字类来显示它。

在一个地方我使用以下

<input type="number" name="searchClientNo" class="numericOnly" /><br />

但在我使用的输入表单中

@Html.EditorFor(m => m.Number, EditorTemplate.TextBox)

我有一个带有以下代码的自定义 EditorFor

<div class="editor-label">
    @Html.Label((ViewData.ModelMetadata.DisplayName??ViewData.ModelMetadata.PropertyName),
        new Dictionary<string, object>
            {
                { "for", ViewData.ModelMetadata.PropertyName }
            })
</div>
<div class="editor-field">
    @Html.TextBox("", (object)Model,
        new Dictionary<string, object>
            {
                { "id", ViewData.ModelMetadata.PropertyName },
                { "name", ViewData.ModelMetadata.PropertyName },
                { "class", "text-box single-line"},
                { "data-bind", "value: " + ViewData.ModelMetadata.PropertyName },
            })
    @Html.ValidationMessage(ViewData.ModelMetadata.PropertyName,
        new Dictionary<string, object>
            {
                { "data-valmsg-for", ViewData.ModelMetadata.PropertyName }
            })
</div>

我想知道如何保持此代码不变但仍使用仅数字文本框。我需要使用 UIHint 吗?

或者,是否可以让我现有的 EditorFor 更智能?

我找到了这篇博文 http://robseder.wordpress.com/2012/06/01/uihint-displaytemplates-and-editortemplates-in-mvc/,但我已经在使用自定义 EditorFor。可能我需要添加一个新类型,比如 EditorTemplate.NumericTextBox 并添加另一个编辑器?这听起来可能有效,我明天要试试这个......

非常感谢。

【问题讨论】:

    标签: asp.net-mvc entity-framework data-annotations


    【解决方案1】:

    您可以编写自定义编辑器模板~/Views/Shared/EditorTemplates/NumberTemplate.cshtml

    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { type = "number" })
    

    然后使用UIHint 属性装饰您的视图模型属性:

    [Required]
    [DisplayName("Client No")]
    [Column("client_no", TypeName = "smallint")]
    [UIHint("NumberTemplate")]
    public virtual Int16 Number { get; set; }
    

    在你的视野中:

    @Html.EditorFor(x => x.Number)
    

    或者如果您不想在视图模型上使用 UIHint 属性,您可以定义 EditorTemplate.NumericTextBox = "NumberTemplate" 然后:

    @Html.EditorFor(m => m.Number, EditorTemplate.NumericTextBox)
    

    【讨论】:

    • 嗨达林,这是我正在考虑实施的解决方案,但想问一下是否有可能在一个特定的 EditorFor(我的通用 tetxbox 类型)中以某种方式根据模型的类型做出决定?我将我当前的代码添加到问题中。
    • 另外,我正在使用@Html.TextBox 并提供属性,但是如果我尝试使用 ,代码会是什么样子?
    • 达林,我还有几个相关的问题。我实现了以下@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { type = "number", @class= "numericonly" }) @**@
    【解决方案2】:

    只是想分享我的解决方案,以防它对某人有所帮助:

    我的新编辑器:

    <div class="editor-label">
        @Html.Label((ViewData.ModelMetadata.DisplayName??ViewData.ModelMetadata.PropertyName),
            new Dictionary<string, object>
                {                
                    { "for", ViewData.ModelMetadata.PropertyName }
                })
    </div>
    
    <div class="editor-field">
       @if (ViewData.ModelMetadata.ModelType.IsNumeric())
       {
        @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { type = "number", @class = "numericOnly"  })      
       }
        else
        {
         @Html.TextBox("", (object)Model,
            new Dictionary<string, object>
                {
                    { "id", ViewData.ModelMetadata.PropertyName },
                    { "name", ViewData.ModelMetadata.PropertyName },
                    { "class", "text-box single-line"},
                    { "data-bind", "value: " + ViewData.ModelMetadata.PropertyName },
                })
        }
    
         @Html.ValidationMessage(ViewData.ModelMetadata.PropertyName,
            new Dictionary<string, object>
                {
                    { "data-valmsg-for", ViewData.ModelMetadata.PropertyName }
                })
    </div>
    

    IsNumeric 扩展方法基于我在 C# MSDN 论坛中找到的代码,这是它的实现:

    /// <summary>
    /// Checks is the object is of numeric type 
            /// see http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/66a7dc8d-f276-4d45-8da4-f8d9857db52c/
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
            public static bool IsNumeric(object obj)
            {
                return (obj == null) ? false : IsNumeric(obj.GetType());
            }
    
            public static bool IsNumeric(this Type type)
            {
                if (type == null)
                    return false;
    
                TypeCode typeCode = Type.GetTypeCode(type);
    
                switch (typeCode)
                {
                    case TypeCode.Byte:
                    case TypeCode.Decimal:
                    case TypeCode.Double:
                    case TypeCode.Int16:
                    case TypeCode.Int32:
                    case TypeCode.Int64:
                    case TypeCode.SByte:
                    case TypeCode.Single:
                    case TypeCode.UInt16:
                    case TypeCode.UInt32:
                    case TypeCode.UInt64:
                        return true;
                }
                return false;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 2018-05-27
      • 2020-03-23
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多