哪个数据注释属性创建此验证属性? [英] Which Data Annotation attribute creates this validation attribute?

查看:84
本文介绍了哪个数据注释属性创建此验证属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个像这样的模型:

Presuming we have a model like so:

public class TheViewModel
{
    public string DateTime? Image_Date { get; set; }
}

它像这样被添加到Razor视图中:

And it is added to a Razor view like so:

Html.TextBoxFor(model => model.Image_Date)

然后在浏览器中呈现以下内容:

Then the following is rendered in the browser:

<input data-val="true" data-val-date="The field Image_Date must be a date." id="Image_Date" name="Image_Date" type="text" value="" />

属性data-val-date是我感兴趣的属性.它显然是由MVC的不引人注目的" jQuery验证集成注入的.

The attribute data-val-date is what I'm interested in. It's plainly being injected in by MVC's "unobtrusive" jQuery validation integration.

例如,[Required(ErrorMessage="This field is required!")]将覆盖标准的字段{0}是必需的".消息.

For instance, [Required(ErrorMessage="This field is required!")] will override the standard "The field {0} is required." message.

尝试失败:

  1. [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")]似乎对客户端验证没有任何作用.

  1. [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")] doesn't appear to do anything to client-side validation.

[DisplayName("...")]更改模式的通配符部分,但显然不会影响模式本身.

[DisplayName("...")] changes the wildcard portion of the pattern, but obviously doesn't affect the pattern itself.

推荐答案

框架添加data-val-date属性,因为该属性的类型为DateTime?.它是HtmlHelper类的GetUnobtrusiveValidationAttributes()方法,实际上会生成所有data-val-*属性.

The data-val-date attribute is added by the framework because the property is type of DateTime?. Its the GetUnobtrusiveValidationAttributes() method of the HtmlHelper class which actually generates all the data-val-* attributes.

请注意,[DataType(DataType.Date, "...")]EditorFor()方法用于添加type="date"属性的属性,该属性又会生成浏览器HTML-5 datepicker(如果浏览器支持),并且与客户端无关验证.

Note that [DataType(DataType.Date, "...")] is an attribute used by the EditorFor() method to add the type="date" attribute which in turn generates the browsers HTML-5 datepicker (if its supported by the browser) and is not related with client side validation.

默认错误消息在资源文件中定义,您可以创建自己的错误消息以覆盖默认值.

The default error messages are defined in Resource files, and you can create you own to override the defaults.

App_GlobalResources文件夹中创建一个(例如)MyResources.resx(您可能需要创建此文件夹),并添加以下FieldMustBeDate键和您的消息(默认消息如下所示)

Create a (say) MyResources.resx in the App_GlobalResources folder (you may need to create this folder) and add the following FieldMustBeDate key and your message (the default message is shown below)

FieldMustBeDate : The field {0} must be a date

,然后在Global.asaxApplication_Start()中添加以下内容

and add the following in the Application_Start() of Global.asax

ClientDataTypeModelValidatorProvider.ResourceClassKey = "MyResources";
DefaultModelBinder.ResourceClassKey = "MyResources";

请注意,您还可以使用PropertyValueRequired键覆盖[Required]属性的默认错误消息

Note you can also override the default error message for the [Required] attribute using the PropertyValueRequired key

这篇关于哪个数据注释属性创建此验证属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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