DataTypeAttribute是显示错误信息 [英] DataTypeAttribute is showing the wrong message

查看:108
本文介绍了DataTypeAttribute是显示错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下属性:

public class SpecificDataTypeAttribute : DataTypeAttribute
{
    public SpecificDataType(DataType dataType, string field)
        : base(dataType)
    {
        this.ErrorMessage = string.Format("{0} {1}", field, Messages.SpecificDataTypeAttribute);
    }
}

和使用,如:

[SpecificDataType(DataType.DateTime, "Initial date")]
public DateTime? InitialDate { get; set; }

所以,这是 Messages.SpecificDataTypeAttribute 的消息是一个不正确的格式。。当我输入一个错误的日期在InitialDate,我得到了默认的错误:值'12'是无效的Ini​​tialDate 。为什么?我把断点和code为调用构造函数SpecificDataType。

So, the message that is in Messages.SpecificDataTypeAttribute is "is in a incorrect format.". When i input a wrong date in InitialDate, i got the default error: "The value '12' is not valid for InitialDate.". Why? I put the breakpoint and the code is calling the SpecificDataType ctor.

推荐答案

您要在错误的方向 - 在asp.net mvc的, DataTypeAttribute 没有定义的验证规则。这或多或少像<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.aspx\"相对=nofollow> UIHintAttribute - 有助于指定编辑或显示模式渲染属性时要使用的模板。

You are going in wrong direction - in asp.net mvc, DataTypeAttribute does not define validation rules. It is more or less like UIHintAttribute - helps to specify which template to use when rendering property in edit or display modes.

看看这个答案以了解定制验证消息的系统类型

Take a look at this answer to learn about customizing validation messages for system types

PropertyValueInvalid 值的格式,替换为{0}由无效的值,而{1}与属性名称。所以,你可以将它定义为

The value for PropertyValueInvalid is formatted, with {0} replaced by invalid value, and {1} with property name. So you can define it as

{1}是无效的格式

这篇关于DataTypeAttribute是显示错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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