我的Page fromDate和ToDate中有两个日期字段。 ToDate应大于FromDate。我需要在客户端验证它。 我正在使用Foolprof进行clied side validation
参考添加
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>
现在我的验证在客户端无效,但在单击提交按钮后在服务器端工作。
请给我一些指导..
谢谢。
答案 0 :(得分:1)
我认为你没有引用jQuery库。如果您使用的是Unobtrusive,则应使用以下脚本
<script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/mvcfoolproof.unobtrusive.js")" type="text/javascript"></script>
如果您正在使用jQuery验证,请将MvcFoolproofJQueryValidation.js包含在标准客户端验证脚本文件中
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-validate.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>
<script src="../../Scripts/MvcFoolproofJQueryValidation.js" type="text/javascript"></script>