2012-12-21 41 views
6

我想弄清楚是否有一种方法可以确保仅使用数据注解和实体框架的数字输入。如何使输入字段仅允许使用EF和数据注释的数字?

我使用下面的代码

[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) 

在那里我有下面的代码

<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> 
做出EditorFor定制

我想知道如何保持此代码没有变化,但仍然使用数字只有文本框。我需要使用UIHint吗?

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

我发现此博客文章http://robseder.wordpress.com/2012/06/01/uihint-displaytemplates-and-editortemplates-in-mvc/但我已经在使用自定义的EditorFor。可能是我需要添加一个新类型,例如EditorTemplate.NumericTextBox并添加另一个编辑器?这听起来像它可能工作,我明天会试试这个...

非常感谢提前。

回答

9

你可以写一个自定义编辑模板~/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) 

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

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

嗨达林, 这是我正在考虑实施的解决方案,但想问一个特定的EditorFor(我的通用tetxbox类型)内是否可能以某种方式根据模型的类型作出决定?我将当前的代码添加到问题中。 – Naomi

+0

以及我正在使用@ Html.TextBox并提供属性,但代码将如何看起来像,而不是我会尝试使用? – Naomi

+0

达林, 我还有几个相关的问题。我实现了以下 @ Html.TextBox(“”,ViewData.TemplateInfo.FormattedModelValue,new {type =“number”,@class =“numericonly”}) @ * input type =“number”id =“@ ViewData .ModelMetadata.PropertyName“name =”@ ViewData.ModelMetadata.PropertyName“class =”numericOnly“value =”@ Model“ data-bind =”@ ViewData.ModelMetadata.PropertyName“/> * @ – Naomi

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; 
     } 
相关问题