试图强制进行远程验证

时间:2015-05-26 08:53:06

标签: jquery asp.net-mvc razor remote-validation

使用远程验证来比较模型的开始和结束时间。

一切正常,但发现了一个小虫。

如果错误地将开始时间设置为11am,则输入结束时间为上午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>

1 个答案:

答案 0 :(得分:0)

将Jquery更改为

<script>
    $('#MondayStart').change(function (event) {
        $("#MondayEnd").removeData("previousValue");
        $('#MondayEnd').valid();
    })
</script>
<script>
    $('#MondayEnd').change(function (event) {
        $("#MondayStart").removeData("previousValue");
        $('#MondayStart').valid();
    })
</script>

它有效吗