リモート検証を使用して、モデルの開始時刻と終了時刻を比較します。
すべて正常に動作しましたが、小さなバグが見つかりました。
開始時刻が誤って午前 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>