【问题标题】:mvc 5 validate date in the pastmvc 5 验证过去的日期
【发布时间】:2016-06-15 21:48:00
【问题描述】:

我在这里看到了问题,但我无法解决问题。

我希望当用户在表单中插入日期时,只有过去的日期才是相关的。

例子:

用户不能在 02/03/2016 之后插入日期。

我的模特:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Date")]
[Required(ErrorMessage = "Date is mandatory")]
public DateTime created_date { get; set; }

我的看法:

<div class="form-group" >
    @Html.LabelFor(model => model.created_date, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10" >
        @Html.EditorFor(model => model.created_date, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.created_date, "", new { @class = "text-danger" })
    </div>
</div>

谢谢。

【问题讨论】:

  • 旁注:由于您似乎需要 HTML5 浏览器日期选择器(仅在 Chrome 和 Edge 中支持),格式必须为 DataFormatString = "{0:yyyy-MM-dd}" 才能正常工作。您应该考虑使用自定义验证属性来获取客户端和服务器端验证。

标签: c# html asp.net asp.net-mvc asp.net-mvc-5


【解决方案1】:

您可以使用HTML 5 日期类型作为示例:

<input type="date" max="2016-03-02">

https://jsfiddle.net/nsvno84t/1/

使用 Razor 执行此操作,它将为您提供今天的日期:

<input type="date" max='@DateTime.Now.ToString("dd/MM/yyyy")'>

但是,您可能希望确定用户/服务器的日期时间设置,因为这适用于英国格式(在您的情况下,@DateTime.Now.ToString("yyyy-MM-dd") 之类的可能更好)。

您还应该针对服务器和客户端验证它,在您的 MVC 代码中您可以使用ValidationAttribute

public class RestrictedDate : ValidationAttribute
{
   public override bool IsValid(object date) 
   {
       DateTime date = (DateTime)date;
       return date < DateTime.Now;
   }
}

然后在您的模型上使用新的RestrictedDate 属性:

public class YourModel
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    [Display(Name = "Date")]
    [Required(ErrorMessage = "Date is mandatory")]
    [RestrictedDate]
    public DateTime created_date { get; set; } 
}

作为附加说明,您可能还需要考虑将 created_date 更改为 CreatedDate 以符合 C# 命名约定。

【讨论】:

  • 我在模型里写这个类?还是在模型文件夹中?我在这一行有错误 DateTime date = (DateTime)date;
  • 为什么我不能提到@Darren Davies?
  • 你能帮我吗@Darren Davies?
  • @MaorAzulay - 我会在 Validation 或 Attributes 文件夹中编写 RestrictedDate 类,然后在模型中引用新类。您收到的错误是什么? InvalidCast 异常?如果是这样,请尝试 DateTime.TryParse,如果您有其他错误,请发布一个新问题,我会尽力帮助您:)
【解决方案2】:

我建议使用条件语句在控制器中进行验证。我不知道您是否在 CreateEdit 视图中,但在您的控制器中,您可以像这样进行验证:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID, created_date" /* and whatever else you have in your model*/)] YourModel yourModel)

if(ModelState.IsValid)
{
    if (yourModel.created_date > DateTime.Today)
    {
        ModelState.AddModelError("created_date", "Date cannot be set to future date!");
        return View(yourModel);
    }

    return View(yourModel);
}

但这是针对服务器进行验证..所以我还建议将该验证与@Darren Davies 在他的回答中给出的客户端验证结合起来..

【讨论】:

    【解决方案3】:

    首先你必须改变

    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]

    到 [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

    因为某些浏览器不能给出完美的结果

    【讨论】:

    • 你能给我解释一下吗?我使用 jquery
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2014-10-17
    • 2017-09-26
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多