如何使输入字段允许数字只能使用EF和数据注释? [英] How to make entry field to allow numbers only using EF and Data Annotations?

查看:110
本文介绍了如何使输入字段允许数字只能使用EF和数据注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设法确定是否有办法确保使用数据注释和实体框架允许使用数字输入。

I am trying to figure out if there is a way to make sure that numeric only input is allowed using Data Annotations and Entity Framework.

我正在使用以下内容代码

I am using the following code

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

我希望使用数字类显示。

I want this to be displayed using number class.

在一个地方,我使用以下

In one place I use the following

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

但是在我使用的输入表单中

but in the entry form I am using

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

我有一个自定义编辑器与以下代码

where I have a custom made EditorFor with the following code

<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吗?

I am wondering how can I keep this code without changes but still use the numeric only textbox. Do I need to use UIHint?

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

Or alternatively, is it possible to make my existing EditorFor smarter?

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

I found this blog post http://robseder.wordpress.com/2012/06/01/uihint-displaytemplates-and-editortemplates-in-mvc/ but I am already using a custom EditorFor. May be I need to add a new type, say, EditorTemplate.NumericTextBox and add another editor? This sounds like it may work, I am going to try this tomorrow...

提前感谢很多。

推荐答案

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

You could write a custom editor template ~/Views/Shared/EditorTemplates/NumberTemplate.cshtml:

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

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

and then decorate your view model property with the UIHint attribute:

[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然后:

or if you don't want to use the UIHint attribute on your view model you could define EditorTemplate.NumericTextBox = "NumberTemplate" and then:

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

这篇关于如何使输入字段允许数字只能使用EF和数据注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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