传递字符串使用数据注释错误信息 [英] Pass string for error message using Data Annotations

查看:156
本文介绍了传递字符串使用数据注释错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误消息。

string errorMessage="Something happened";

[Remote("IsTimeValid", "Account", AdditionalFields = "TaskDate,TodoID", ErrorMessage =errorMessage)]
public string Time{get;set;}

有什么办法来显示ErrorMessages变量值?

Is there any way to show variable value in ErrorMessages?

推荐答案

您需要做的pretty多,你要问什么(在您的评论)的能力。

You have the ability to do pretty much exactly what you're asking (in your comment).

您设置 ErrorMessageResourceType 是包含静态属性的类型和设置 ErrorMessageResourceName 是名称在类属性

You set ErrorMessageResourceType to be the type which contains the static property and you set ErrorMessageResourceName to be the name of the property on the class

例如

public class Time
{
    [Remote("IsTimeValid", "Account", AdditionalFields = "TaskDate,TodoID", ErrorMessageResourceType = typeof(Time), ErrorMessageResourceName = "InvalidTimeZoneMessage")]
    public string Time{get;set;}

    public static string InvalidTimeZoneMessage
    {
        get
        {
            return "You have the wrong timezone";
        }
    }
}

这篇关于传递字符串使用数据注释错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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