【发布时间】:2014-05-27 11:58:32
【问题描述】:
我的页面中有两个日期字段 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>
现在我的验证不在客户端工作,而是在单击提交按钮后在服务器端工作。
请给我一些指导..
谢谢。
【问题讨论】:
-
你应该用javascript来做。在提交按钮上添加一个 onclick 事件,该事件将在提交表单之前检查开始日期和结束日期。
-
感谢您的回复。实际上我的要求是使用 FoolProof 来实现这一点。
-
发布日期字段的 html
-
@StephenMuecke 用 cshtml 代码编辑了我的问题。谢谢你。
-
您的 cshtml 代码看起来一切正常。您是否按照 Jay 的回答检查了 js 文件?
标签: jquery asp.net-mvc validation foolproof-validation