我的页面 fromDate 和 ToDate 中有两个日期字段。ToDate 应该大于 FromDate。我需要在客户端验证它。我正在使用 Foolprof 进行侧边验证
添加了参考
using Foolproof;
添加了脚本
<script src="~/Scripts/mvcfoolproof.unobtrusive.min.js"></script> <script src="~/Scripts/MvcFoolproofJQueryValidation.min.js"></script> <script src="~/Scripts/MvcFoolproofValidation.min.js"></script>
我的模型包含以下代码
[Required(ErrorMessage = "The start date is required")] public DateTime StartDate { get; set; } [Required(ErrorMessage = "The end date is required")] [GreaterThan("StartDate")] public DateTime EndDate { get; set; }
并在控制器中使用默认脚手架完成。
我的 .cshtml 包含以下日期代码
<div class="form-group">
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
现在我的验证不在客户端工作,而是在单击提交按钮后在服务器端工作。
请给我一些指导..
谢谢你。