/// <summary>
/// Specifies that the field must compare favourably with the named field, if objects to check are not of the same type
/// false will be return
/// </summary>
public class CompareValuesAttribute : ValidationAttribute
{
/// <summary>
/// The other property to compare to
/// </summary>
public string OtherProperty { get; set; }
public CompareValues Criteria { get; set; }
/// <summary>
/// Creates the attribute
/// </summary>
/// <param name="otherProperty">The other property to compare to</param>
public CompareValuesAttribute(string otherProperty, CompareValues criteria)
{
if (otherProperty == null)
throw new ArgumentNullException("otherProperty");
OtherProperty = otherProperty;
Criteria = criteria;
}
/// <summary>
/// Determines whether the specified value of the object is valid. For this to be the case, the objects must be of the same type
/// and satisfy the comparison criteria. Null values will return false in all cases except when both
/// objects are null. The objects will need to implement IComparable for the GreaterThan,LessThan,GreatThanOrEqualTo and LessThanOrEqualTo instances
/// </summary>
/// <param name="value">The value of the object to validate</param>
/// <param name="validationContext">The validation context</param>
/// <returns>A validation result if the object is invalid, null if the object is valid</returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// the the other property
var property = validationContext.ObjectType.GetProperty(OtherProperty);
// check it is not null
if (property == null)
return new ValidationResult(String.Format("Unknown property: {0}.", OtherProperty));
// check types
var memberName = validationContext.ObjectType.GetProperties().Where(p => p.GetCustomAttributes(false).OfType<DisplayAttribute>().Any(a => a.Name == validationContext.DisplayName)).Select(p => p.Name).FirstOrDefault();
if (memberName == null)
{
memberName = validationContext.DisplayName;
}
if (validationContext.ObjectType.GetProperty(memberName).PropertyType != property.PropertyType)
return new ValidationResult(String.Format("The types of {0} and {1} must be the same.", memberName, OtherProperty));
// get the other value
var other = property.GetValue(validationContext.ObjectInstance, null);
// equals to comparison,
if (Criteria == CompareValues.EqualTo)
{
if (Object.Equals(value, other))
return null;
}
else if (Criteria == CompareValues.NotEqualTo)
{
if (!Object.Equals(value, other))
return null;
}
else
{
// check that both objects are IComparables
if (!(value is IComparable) || !(other is IComparable))
return new ValidationResult(String.Format("{0} and {1} must both implement IComparable", validationContext.DisplayName, OtherProperty));
// compare the objects
var result = Comparer.Default.Compare(value, other);
switch (Criteria)
{
case CompareValues.GreaterThan:
if (result > 0)
return null;
break;
case CompareValues.LessThan:
if (result < 0)
return null;
break;
case CompareValues.GreatThanOrEqualTo:
if (result >= 0)
return null;
break;
case CompareValues.LessThanOrEqualTo:
if (result <= 0)
return null;
break;
}
}
// got this far must mean the items don't meet the comparison criteria
return new ValidationResult(ErrorMessage);
}
}
/// <summary>
/// Indicates a comparison criteria used by the CompareValues attribute
/// </summary>
public enum CompareValues
{
EqualTo,
NotEqualTo,
GreaterThan,
LessThan,
GreatThanOrEqualTo,
LessThanOrEqualTo
}
复制代码
应用的时候直接在指定的属性上添加此CompareValuesAttribute标签即可
【注:第一个参数是要与之比较的属性名,第二个参数表示两个属性值之间的大小关系,第三个参数表示错误提示信息】
复制代码
public class EricSunModel
{
[Display(Name = "Ready Time")]
public string ReadyTime { get; set; }
[CompareValues("ReadyTime", CompareValues.GreaterThan, ErrorMessage = "Close time must be later than ready time")]
[Display(Name = "Close Time")]
public string CloseTime { get; set; }
- MVC日期格式化的2种方式
原文:MVC日期格式化的2种方式 假设有这样的一个类,包含DateTime类型属性,在编辑的时候,如何使JoinTime显示成我们期望的格式呢? using System; using System. ...
- MVC日期格式化,后台使用Newtonsoft.Json序列化日期,前端使用”f”格式化日期
MVC控制器中,经常使用Newtonsoft.Json把对象序列化成json字符串传递到前端视图.当对象中有DateTime类型的属性时,前后台如何处理才能把DateTime类型转换成想要的格式呢? ...
- spring mvc日期转换(前端到后端,后端到前端)
在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置. 1.如果查询类使我们自己写,那么在属性前面加上@Date ...
- mvc日期控件datepick的几篇文章,日后再总结吧
instinctcoder里有两篇,入门级的 http://instinctcoder.com/asp-net-mvc-4-jquery-datepicker/ http://instinctcode ...
- MVC日期和其它字符串格式化
-- (月份位置不是03) string.Format("{0:D}",System.DateTime.Now) 结果为:2009年3月20日 : :: -- : -- :: st ...
- spring的mvc对于页面日期格式进行传值到后台
对于spring的mvc 日期格式从页面传入后台是个问题.string类型和整形都能友好传入.但是对于日期类型date却不能传入.回报403参数不对的错误. 看例子: @RequestMapping( ...
- SpringBoot系列——Jackson序列化
前言 Spring Boot提供了与三个JSON映射库的集成: Gson Jackson JSON-B Jackson是首选的默认库. 官网介绍: https://docs.spring.io/spr ...
- spring mvc 获取页面日期格式数据
1.传递日期参数: 解决办法: 实体类日期属性加 @DateTimeFormat(pattern="yyyy-MM-dd") 注解 beans中加 <mvc:annotati ...
- spring mvc 注解访问控制器以及接收form数据的方式,包括直接接收日期类型及对象的方法
Spring 中配置扫描器 <!-- springmvc的扫描器--> <context:component-scan base-package="com.beifeng. ...
随机推荐
- VUE中的v-if与v-show
1.共同点 都是动态显示DOM元素 2.区别 (1)手段:v-if是动态的向DOM树内添加或者删除DOM元素:v-show是通过设置DOM元素的display样式属性控制显隐: (2)编译过程:v-i ...
- IE8,9下的ajax缓存问题
最近在做一个网站的登录注册框,前端使用了jquery.由于sign和login不是在单独的页面上,而是以一个弹出框出现.所以决定使用ajax来实现注册和登录功能.本以为可以一帆风顺,结果在测试的时候发 ...
- Linux菜鸟之路[4]-cal,date,bc,echo $LANG,man
由于前四天一直在看鸟哥的linux书本的计算机的一些基础知识,今天才接触基本的命令,从今天起每天记录一下自己的linux学习过程. cal:日历 cal: cal 2015:列出2015年所有日历 c ...
- oracle dataguard 角色切换
- Mac经常使用快捷键
Mac使用快捷键会节省非常多时间.使用最多的键就是shift键 option键 command键的组合了.当然一下略微用得多一点点,还有非常多快捷键没一一列举了 进入指定文件夹的一些快捷键 进入 A ...
- [Codecademy] HTML&CSS 第三课:HTML Basic II
本文出自 http://blog.csdn.net/shuangde800 [Codecademy] HTML && CSS课程学习目录 --------------------- ...
- c/c++测试程序运行时间
算法分析中需要对各种算法进行性能测试,下面介绍两种通用的测试方法,由于只用到标准c语言函数,所以在各种平台和编译器下都能使用. 方法1: clock()函数 开始计时:start = clock() ...
- linux 下手动编译安装无线网卡驱动
先参照 <本地yum源安装GCC >安装好gcc hp的笔记本上安装了CentOS6.3,没有安装无线网卡驱动,安装这个驱动,在Google上找了好多资料,最后终于解决了这个问题.在这里做 ...
- 上传多张图片用Session临时存储
DataTable dtImages = new DataTable(); string filepath = FileUpload1.PostedFile.FileName; //检查是否有文件要上 ...
- 浅谈JSP(一)
一.JSP引言 JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计.它是在传统的网页HTML文件(*.htm,*.html)中插入Java ...