大于和小于同一属性的数据注释

时间:2014-08-19 14:59:23

标签: asp.net-mvc-4 data-annotations foolproof-validation

我正在尝试对具有DateTime作为类型的属性执行某个条件,它必须大于同一个Object的另一个属性而不是另一个属性也是同一个Object的属性。我的问题是当我尝试时要应用这两个DataAnnotation,它会给我这个错误:

  

不显眼的客户端验证规则中的验证类型名称必须是唯一的。以下验证类型不止一次出现:是

我在尝试使用Range DataAnnotation后尝试了这个,我知道这不是最好的方法,但Range只是没有为我工作它总是给我这个错误:

  

字符串未被识别为有效的DateTime。从索引0开始有一个未知单词。

这是我的模型(我使用实体框架来生成我的模型,所以我使用partialclasses来做验证技巧):

 [MetadataType(typeof(AffectationMetadata))]
public partial class AFFECTATION
{

    public DateTime DateDebutVisite { set; get; }

    public DateTime DateFinVisite { set; get; }



    public class AffectationMetadata
    {
        //some other entities

        [Required(ErrorMessage = "Date de début de visite est obligatoire.")]
        [Display(Name = "Date du projet dans la période de la visite * :")]
        [GreaterThan("DateDebutVisite")]
        [LessThan("DateFinVisite")]
        public DateTime DATEAFFECTATION { set; get; }


    }
}

有人可以告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

所以这里有2个问题。首先,Foolproof验证只能看到同一类中的属性。所以你的课程需要看起来像这样:

<div id="shape">
  <span id="shape-content">
    Now, the fact is, if it's a woman in front of you that's
    writing the cheque, you will not be waiting long. I have noticed that
    women are very *fast* with cheques, y'know, 'cuz they write out so many
    cheques. The keys, they can never find in their purse, they don't know
    where that is, but the cheque book they got that. They never fumble for
    the cheque book-- the cheque book comes out of a holster: ["draws" imaginary
    book from an imaginary holster] ``Who do I make it out to?... There's my
    ID...''. There's something about a cheque that, to a man, is not masculine.
    I don't know exactly what it is... I think to a man, a cheque is like a note
    from your mother that says ``I don't have any money, but if you'll
    contact these people, I'm sure they'll stick up for me... If you
    just trust me this one time I don't have any money but I have
    these... I wrote on these; is this of any value at all?''
    </span>
</div>

第二个问题,这真的是整个事情的关键是[MetadataType(typeof(AffectationMetadata))] public partial class AFFECTATION { public class AffectationMetadata { public DateTime DateDebutVisite { set; get; } public DateTime DateFinVisite { set; get; } //some other entities [Required(ErrorMessage = "Date de début de visite est obligatoire.")] [Display(Name = "Date du projet dans la période de la visite * :")] [GreaterThan("DateDebutVisite")] [LessThan("DateFinVisite")] public DateTime DATEAFFECTATION { set; get; } } } GreaterThanAttribute实际上只是LessThanAttribute的子类,因此它看到了2个相同的问题属性在抛出错误的同一属性上。我现在正在寻找解决这个问题的方法。如果我找到答案,我会编辑这个答案。