ASP.NET MVC 的注解

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;


namespace 注解的应用.Models
{
public class User
{
[HiddenInput(DisplayValue = false)]
//[ScaffoldColumn(false)]
[Display(Name="编号")]
public int Id { get; set; }

[Display(Name="用户名")]
//[StringLength(10)]
//[Common.MyStringLength(10,ErrorMessage="{0}字数太多了!")]
//[Common.MyStringLength(5)]
[Common.MyStringLength(5,ErrorMessage="{0}字符太少了")]
[Required(ErrorMessage="请输入用户名")]
[Remote("CheckValid", "Home", AdditionalFields = "UserName,Pwd",ErrorMessage="该用户已存在,请重新输入")]
public string UserName { get; set; }


[DataType(DataType.Password)]
[Display(Name="密码")]
[System.ComponentModel.DataAnnotations.Compare("UserName",ErrorMessage="密码不一致")]
public string Pwd { get; set; }

[DisplayFormat(DataFormatString="{0:C}",ApplyFormatInEditMode=false)]
[Range(10,30)]
public decimal Price { get; set; }

//使用DataType(DataType.EmailAddress,ErrorMessage="请输入正确的Email地址")自定义的错误消息不会生效
//[DataType(DataType.EmailAddress,ErrorMessage="请输入正确的Email地址")]
[RegularExpression(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "请输入正确的Email地址")]
public string Email { get; set; }
}
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace 注解的应用.Common
{
/// <summary>
/// 自定义注解类,自定义注解不支持客户端验证,在模型绑定时执行IsValid方法执行验证逻辑,
/// 如果不调用FormatErrorMessage方法进行格式化,传入格式化的错误消息,则在
/// [Common.MyStringLength(10,ErrorMessage="{0}字数太多了!")]中自定义的消息不回生效
/// </summary>
public class MyStringLengthAttribute:ValidationAttribute
{
private readonly int MaxLength;
public MyStringLengthAttribute(int maxLength)
{
this.MaxLength = maxLength;
}

//protected override ValidationResult IsValid(object value, ValidationContext validationContext)
//{

// string content = value.ToString();
// if (content.Length > MaxLength)
// {
// //return new ValidationResult("输入的字符太多了!^_^");

// string errorMessage = FormatErrorMessage(validationContext.DisplayName);
// return new ValidationResult(errorMessage);
// }
// return ValidationResult.Success;
// //return base.IsValid(value, validationContext);
//}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
if (value.ToString().Length < MaxLength)
{
//return new ValidationResult("请输入" + MaxLength + "位以上长度的字符!");
string errorMessage = FormatErrorMessage(validationContext.DisplayName + "请输入" + MaxLength + "位以上长度的字符!");
return new ValidationResult(errorMessage);
}
}
return ValidationResult.Success;
}

}
}

转载于:https://www.cnblogs.com/zizhu1226/p/4065060.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值