【问题标题】:Datetime required (+18 years)所需日期时间(+18 年)
【发布时间】:2016-11-04 02:07:57
【问题描述】:

我有一个小问题,这是我的代码:

public partial class Tourist
    {

        public Tourist()
        {
            Reserve = new HashSet<Reserve>();
        }
        public int touristID { get; set; }

        [Required]
        [StringLength(50)]
        public string touristNAME { get; set; }

        public DateTime touristBIRTHDAY { get; set; }

        [Required]
        [StringLength(50)]
        public string touristEMAIL { get; set; }

        public int touristPHONE { get; set; }

        public virtual ICollection<Reserve> Reserve { get; set; }
    }
}

如何将 TouristBIRTHDAY 限制为 +18 岁?我想我必须使用这个功能,但我不知道放在哪里: 注意:这个函数是一个例子。

DateTime bday = DateTime.Parse(dob_main.Text);
DateTime today = DateTime.Today;
int age = today.Year - bday.Year;
if(age < 18)
{
    MessageBox.Show("Invalid Birth Day");
}

谢谢 ;)

更新: 我遵循 Berkay Yaylaci 的解决方案,但我得到了 NullReferenceException。看来我的value参数是默认的,然后我的方法没有发布,为什么?解决办法是什么?

【问题讨论】:

    标签: asp.net-mvc datetime required


    【解决方案1】:

    您可以编写自己的验证。首先,创建一个类。

    我调用了 MinAge.cs

     public class MinAge : ValidationAttribute
        {
            private int _Limit;
            public MinAge(int Limit) { // The constructor which we use in modal.
                this._Limit = Limit;
            }
            protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
            {
                    DateTime bday = DateTime.Parse(value.ToString());
                    DateTime today = DateTime.Today;
                    int age = today.Year - bday.Year;
                    if (bday > today.AddYears(-age))
                    {
                       age--; 
                    }
                    if (age < _Limit)
                    {
                        var result = new ValidationResult("Sorry you are not old enough");
                        return result; 
                    }
                   
                
                return null;
    
            }
        }
    

    SampleModal.cs

    [MinAge(18)] // 18 is the parameter of constructor. 
    public DateTime UserBirthDate { get; set; }
    

    IsValid 在发布后运行并检查限制。如果年龄不大于限制(我们在模态中给出!),则返回 ValidationResult

    【讨论】:

    • 谢谢!我按照你说的做了,但有些事情不是很好。例如,我创建了一个 Tourist 1,其中生日是:10-05-1990,它工作得很好。然后我尝试创建一个 Tourist 2,其中生日是 12-22-1994,它应该可以正常工作,但结果却是“用户代码未处理 NullReferenceException”。为什么?注意: NullReferenceException 在“DateTime bday = DateTime.Parse(value.ToString());”时弹出
    • @ElHashashin 我猜格式错误。你检查了第一个的格式吗?是 10 月 22 日还是 12 月 12 日?
    • @ElHashashin 对于成功的人来说,年龄变量应该是 20。是吗?那条线上什么是空的?价值?
    • 我只是希望游客必须+18岁才能做储备。我找不到游客 1 工作而游客 2 返回异常的原因......这两位游客,在计算中,都有超过 18 年
    • 你可以在this answer看到正确的代码:)
    【解决方案2】:

    在您的旅游类上实现 IValidatableObject。

    将您的逻辑放入 Validate() 方法中。

    您正在使用 MVC,因此没有 MessageBox.Show()。 MVC 模型绑定器会自动调用您的验证例程。

    这是另一个带有详细信息的 SO 问题How do I use IValidatableObject?

    你的年龄逻辑也是错误的。它必须是

    DateTime now = DateTime.Today;
    int age = now.Year - bday.Year;
    if (now < bday.AddYears(age)) age--;
    

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 2021-01-07
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 2019-02-20
      相关资源
      最近更新 更多