C# 必填字段-条件表达式/验证

C# 必填字段-条件表达式/验证,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我想知道如何对DateTime属性设置条件要求。也就是说,除了检查这个必填字段是否为空之外,我希望输入(在cshtml文件中)不早于从今天开始的3周 型号: [DataType(DataType.Date)] [Display(Name = "Start date"), Required(ErrorMessage = ValidationMessages.IsRequired)] //[What else here for this condition??] public DateTime St

我想知道如何对DateTime属性设置条件要求。也就是说,除了检查这个必填字段是否为空之外,我希望输入(在cshtml文件中)不早于从今天开始的3周

型号:

[DataType(DataType.Date)]
[Display(Name = "Start date"), Required(ErrorMessage = ValidationMessages.IsRequired)]
//[What else here for this condition??]
public DateTime StartDate { get; set; }
.cshtml:

<div class="form-group">
    <div class="editor-label">
        @Html.LabelFor(model => model.Assignment.StartDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Assignment.StartDate)
        @Html.ValidationMessageFor(model => model.Assignment.StartDate)
    </div>
</div>

@LabelFor(model=>model.Assignment.StartDate)
@EditorFor(model=>model.Assignment.StartDate)
@Html.ValidationMessageFor(model=>model.Assignment.StartDate)
这样的条件表达式是什么样子的?我需要在模型中添加条件以外的内容吗

如果我的描述太少,请告诉我

//
提前感谢,您可以在您的模型中执行此操作。添加正确的错误消息

[Required(ErrorMessage = "")]

[Range(typeof(DateTime), DateTime.Now.ToString(), DateTime.Now.AddDays(21).ToString(), ErrorMessage = "" )]

public DateTime StartDate { get; set; }

您可以创建自己的验证属性,如下所示:

1) 带有自定义错误消息的自定义验证属性

public class CheckInputDateAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var inputDate = (DateTime)value;
        var compareDate = DateTime.Now.AddDays(21);
        int result = DateTime.Compare(inputDate, compareDate);
        const string sErrorMessage = "Input date must be no sooner than 3 weeks from today.";
        if (result < 0)
        {
            return new ValidationResult(sErrorMessage);
        }
        return ValidationResult.Success;
    }
}
public class CheckInputDateAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var inputDate = (DateTime)value;
        var compareDate = DateTime.Now.AddDays(21);
        int result = DateTime.Compare(inputDate, compareDate);
        return result >= 0;
    }
}
2) 没有自定义错误消息的自定义验证属性

public class CheckInputDateAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var inputDate = (DateTime)value;
        var compareDate = DateTime.Now.AddDays(21);
        int result = DateTime.Compare(inputDate, compareDate);
        const string sErrorMessage = "Input date must be no sooner than 3 weeks from today.";
        if (result < 0)
        {
            return new ValidationResult(sErrorMessage);
        }
        return ValidationResult.Success;
    }
}
public class CheckInputDateAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var inputDate = (DateTime)value;
        var compareDate = DateTime.Now.AddDays(21);
        int result = DateTime.Compare(inputDate, compareDate);
        return result >= 0;
    }
}
然后像这样使用它

  [DataType(DataType.Date)]
  [CheckInputDate]
  public DateTime StartDate { get; set; }
    [DataType(DataType.Date)]
    [Display(Name = "Start date")]   
    [CheckInputDate]
    public DateTime StartDate { get; set; }

看这篇文章,如果我错了,请纠正我的错误,但这不会生成编译错误:“属性参数必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式”?我会尝试一下。不过明天才能试。一个问题,这看起来必须在今天到三周之前,我错了吗?谢谢,我会试试。谢谢,这很有效。然而,我觉得有点傻,因为我没有弄明白这一点。我想显示显示名称和错误消息,而不是显示属性名称和消息:“[Assignment.StartDate]=输入日期必须不早于从今天开始的3周”。您好,很抱歉,回复太晚。但你更新的答案解决了这个问题。谢谢,此帖子可以设置为“已解决”。