7

データ注釈とエンティティ フレームワークを使用して、数値のみの入力が許可されていることを確認する方法があるかどうかを把握しようとしています。

次のコードを使用しています

[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 などの新しいタイプを追加し、別のエディターを追加する必要があるのでしょうか? これは使えそうなので、明日やってみます…

よろしくお願いします。

4

2 に答える 2

10

カスタム エディター テンプレートを作成できます~/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)
于 2012-12-21T10:38:00.483 に答える
1

それが誰かを助けるかもしれない場合に備えて、私の解決策を共有したいだけです:

私の新しい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">
   @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;
        }
于 2013-01-16T13:37:06.490 に答える