热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MVC日期比较(转)

Specifiesthatthefieldmustcomparefavourablywiththenamedfield,ifobjectstocheckarenotofthes

 ///

    /// 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

    ///

    public class CompareValuesAttribute :
ValidationAttribute

    {

        ///

        /// The other property to compare to

        ///

        public string OtherProperty { get; set; }

 

        public CompareValues Criteria { get; set;
}

 

        ///

        /// Creates the attribute

        ///

        /// The other
property to compare to

        public CompareValuesAttribute(string
otherProperty, CompareValues criteria)

        {

            if (otherProperty == null)

                throw new
ArgumentNullException("otherProperty");

 

            OtherProperty =
otherProperty;

            Criteria = criteria;

        }

 

        ///

        /// 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

        ///

        /// The value of the
object to validate

        /// The
validation context

        /// A validation result if the
object is invalid, null if the object is valid

        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().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);

        }

    }

 

    ///

    /// Indicates a comparison criteria used by the CompareValues
attribute

    ///

    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日期比较(转),布布扣,bubuko.com


推荐阅读
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了Foundation框架中一些常用的结构体和类,包括表示范围作用的NSRange结构体的创建方式,处理几何图形的数据类型NSPoint和NSSize,以及由点和大小复合而成的矩形数据类型NSRect。同时还介绍了创建这些数据类型的方法,以及字符串类NSString的使用方法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • 本文介绍了ASP.NET Core MVC的入门及基础使用教程,根据微软的文档学习,建议阅读英文文档以便更好理解,微软的工具化使用方便且开发速度快。通过vs2017新建项目,可以创建一个基础的ASP.NET网站,也可以实现动态网站开发。ASP.NET MVC框架及其工具简化了开发过程,包括建立业务的数据模型和控制器等步骤。 ... [详细]
  • 本文介绍了2015年九月八日的js学习总结及相关知识点,包括参考书《javaScript Dom编程的艺术》、js简史、Dom、DHTML、解释型程序设计和编译型程序设计等内容。同时还提到了最佳实践是将标签放到HTML文档的最后,并且对语句和注释的使用进行了说明。 ... [详细]
author-avatar
和雅竹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有