使用远程验证来比较模型的开始时间和结束时间。
一切正常,但发现了一个小错误。
如果开始时间错误地设置为上午 11 点,则输入结束时间为上午 10 点,结束时间是标志错误,它在开始时间之前。因此,如果我将开始时间更改为上午 9 点,虽然它是正确的,但结束时间仍然显示错误,因为在更改开始时间时尚未重新评估它。可以简单地单击结束时间并关闭标签,但是最终用户会抱怨。
因此尝试根据其他一些帖子再次强制验证,但无法使其正常工作。模型
public bool MondayTrue { get; set; }
[DataType(DataType.Time, ErrorMessage = "Incorrect time")]
[RequiredIfTrue("MondayTrue", ErrorMessage ="Day has been marked as working")]
[DisplayName("Start")]
[Remote("MondayTime", "Remote", AdditionalFields = "MondayEnd", HttpMethod = "POST", ErrorMessage = " * Must be before End time")]
public DateTime? MondayStart { get; set; }
[DataType(DataType.Time, ErrorMessage = "Incorrect time")]
[RequiredIfTrue("MondayTrue", ErrorMessage = "Day has been marked as working")]
[Remote("MondayTime", "Remote", AdditionalFields = "MondayStart", HttpMethod = "POST", ErrorMessage = " * Must be after Start time")]
[DisplayName("End")]
public DateTime? MondayEnd { get; set; }
public bool TuesdayTrue { get; set; }
[DataType(DataType.Time, ErrorMessage = "Incorrect time")]
[RequiredIfTrue("TuesdayTrue", ErrorMessage = "Day has been marked as working")]
[DisplayName("Start")]
视图开始:
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "myForm", id = "myForm" }))
{
开始和结束时间剃刀代码:
<div style="display: table-row;">
<div style="display: table-cell;", class="tCell">Monday</div>
<div style="display: table-cell;", class="tCell">@Html.CheckBoxFor(model => model.MondayTrue, new { id = "txtMondayTrue" })</div>
</div>
<div style="display: table-row;">
<div style="display: table-cell;", class="tCell">@Html.LabelFor(model => model.MondayStart)</div>
<div style="display: table-cell;", class="tCell">
<div>@Html.EditorFor(model => model.MondayStart, new { htmlAttributes = new { id = "MondayStart", disabled = "disabled" } }) </div>
<div>@Html.ValidationMessageFor(model => model.MondayStart, "", new { @class = "text-danger", id = "MondayStartError" }) </div>
</div>
<div style="display: table-cell;" , class="tCell">@Html.LabelFor(model => model.MondayEnd)</div>
<div style="display: table-cell;" , class="tCell">
<div>@Html.EditorFor(model => model.MondayEnd, new { htmlAttributes = new { id = "MondayEnd", disabled = "disabled" } }) </div>
<div>@Html.ValidationMessageFor(model => model.MondayEnd, "", new { @class = "text-danger", id = "MondayEndError" }) </div>
</div>
</div>
Jquery 我以为我在强迫它重新检查
<script>
$("#txtMondayTrue").click(function (event) {
if ($(this).is(":checked")) {
$('#MondayStart').prop("disabled", false).val("");
$('#MondayEnd').prop("disabled", false).val("");
$('#MondayStartError').show();
$('#MondayEndError').show();
}
else {
$('#MondayStart').prop("disabled", true).val("");
$('#MondayEnd').prop("disabled", true).val("");
$('#MondayStartError').hide();
$('#MondayEndError').hide();
}
});
</script>
<script>
$('#MondayStart').change(function (event) {
$("#MondayEnd").removeData("previousValue");
$('myform').validate().element('#MondayEnd');
})
</script>
<script>
$('#MondayEnd').change(function (event) {
$("#MondayStart").removeData("previousValue");
$('myform').validate().element('#MondayStart');
})
</script>