System.ComponentModel.DataAnnotations dll中标签的用法
2013-09-02 15:59
357 查看
定义数字验证
//IsValid 为false时,会执行FormatErrorMessage
public class CustomDigitAttribute :ValidationAttribute
{
public override bool IsValid(object value)
{
Regex regex = new Regex("^[0-9]*$");
bool isOk = true;
if (value != null)
{
isOk = regex.IsMatch(value.ToString());
}
return isOk ;
}
public override string FormatErrorMessage(string name)
{
string IsDigit = name + SystemConstant.IS_DIGIT;
return LanguageProvider.GetLanguageValue(SystemConstant.SetLanguage(), IsDigit);
}
}
对象中的属性
[CustomDigitAttribute()]
public string Experience { get; set; }
获取对象的Error
Validator.TryValidateObject(instance, new ValidationContext(instance, null, null), validationResults, true);
foreach (ValidationResult validationResult in validationResults)
{
string ss = validationResult.ErrorMessage;
errorBuilder.Append(validationResult.ErrorMessage);
}
//IsValid 为false时,会执行FormatErrorMessage
public class CustomDigitAttribute :ValidationAttribute
{
public override bool IsValid(object value)
{
Regex regex = new Regex("^[0-9]*$");
bool isOk = true;
if (value != null)
{
isOk = regex.IsMatch(value.ToString());
}
return isOk ;
}
public override string FormatErrorMessage(string name)
{
string IsDigit = name + SystemConstant.IS_DIGIT;
return LanguageProvider.GetLanguageValue(SystemConstant.SetLanguage(), IsDigit);
}
}
对象中的属性
[CustomDigitAttribute()]
public string Experience { get; set; }
获取对象的Error
Validator.TryValidateObject(instance, new ValidationContext(instance, null, null), validationResults, true);
foreach (ValidationResult validationResult in validationResults)
{
string ss = validationResult.ErrorMessage;
errorBuilder.Append(validationResult.ErrorMessage);
}
相关文章推荐
- System.ComponentModel.DataAnnotations.Schema.TableAttribute 同时存在于EntityFramework.dll和System.ComponentModel.DataAnnotations.dll中
- System.ComponentModel.DataAnnotations.Schema.TableAttribute 同时存在于EntityFramework.dll和System.Componen
- System.ComponentModel.DataAnnotations.Schema 冲突
- System.ComponentModel.DataAnnotations 命名空间和RequiredAttribute 类
- System.ComponentModel.DataAnnotations表示与数据字段和参数关联的数据类型的枚举
- 第6章 数据注解 System.ComponentModel.DataAnnotations 、注解后台原理、自定义验证ValidationAttribute、IValidatableObject
- System.ComponentModel.DataAnnotations.Schema 冲突
- using System.ComponentModel.DataAnnotations
- using System.ComponentModel.DataAnnotations
- C# System.ComponentModel.Composition中的Export和Import特性标签的简单使用。
- AE10.0开发时 System.ComponentModel.LicenseException' occurred in system.windows.forms.dll
- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin
- “CreateRiaClientFilesTask”任务意外失败。 未能加载文件程序集“System.ComponentModel.DataAnnot...
- [System.ComponentModel.DataObject]
- DataContractJsonSerializer是在System.ServiceModel.Web.dll中,但命名空间却是System.Runtime.Serialization.Json。
- “CreateRiaClientFilesTask”任务意外失败。 未能加载文件程序集“System.ComponentModel.DataAnnotations 处理办法
- 在 System.Data.OleDb.OleDbException 中第一次偶然出现的“System.Data.dll”类型的异常
- winform调用sqlite,打包发布后,在客户机上安装运行却报错:找不到System.Data.SQLite.DLL
- C# System.Data.Sqlite.dll 的注意事项
- 解决:必须安装“System.Workflow.ComponentModel.Design.IIdentifierCreationService”服务,才能成功执行此操作。