【问题标题】:Validate two depending dates验证两个依赖日期
【发布时间】:2017-06-27 16:34:29
【问题描述】:

我有两个需要验证的日期值。这个验证是复合的:一个日期必须比另一个日期早(想想 StartingDate 和 FinishDate)。

我如何对此进行验证?

基于这个post,它似乎构成了类似的情况,一个建议是使用MVC Foolproof Validation。这仍然是最好的选择吗?还有其他方法可以验证此类任务吗?

我的代码在视图中,如果有帮助的话:

<div class="row">
        <div class="col-md-3">
            <div class="row">
                <div class="col-md-5">
                    <span class="glyphicon glyphicon-calendar" aria-hidden="true"></span>
                    <span style="font-size:large;">Início&nbsp;</span>
                </div>
                <div class="col-md-7">
                    @Html.EditorFor(model => model.Inicio, new { htmlAttributes = new { @class = "form-control datetimepicker" } })
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="row">
                <div class="col-md-5">
                    <span class="glyphicon glyphicon-calendar" aria-hidden="true"></span>
                    <span style="font-size:large;">Fim&nbsp;</span>
                </div>
                <div class="col-md-7">
                    @Html.EditorFor(model => model.Fim, new { htmlAttributes = new { @class = "form-control datetimepicker" } })
                </div>
            </div>
        </div>
    </div>

    <script>
<!--jQuery DateTimePicker-->
        jQuery('.datetimepicker').datetimepicker({
            format: 'd/m/Y H:i'
        });

    </script>

【问题讨论】:

  • 是否需要在视图中进行此验证?因为如果这个页面正在被提交,你也可以在控制器中检查这个,如果条件为真则返回错误信息。
  • 该页面正在提交,不,它不一定必须在视图中。你能给我看一个这种验证的模糊例子吗?
  • 所以我明白了.. 你所需要的只是验证 endDate 是否小于 startdate?
  • 正确,并显示一条消息,告知用户错误原因。
  • 我的回答有帮助吗?

标签: asp.net validation date razor asp.net-mvc-5


【解决方案1】:

我不明白为什么一个简单的 JS 解决方案在这里无法提供帮助,所以这里是:

HTML:

<p>Start Date: <input type="text" id="datepicker1"></p>

<p>End Date: <input type="text" id="datepicker2"></p>

<button id="TestCondition">
    Click Me!
</button>

JS:

$(function() {
    $("#datepicker1").datepicker();
});

$(function() {
    $("#datepicker2").datepicker();
});

$("#TestCondition").click(function(){
    alert($("#datepicker1").val());
    alert($("#datepicker2").val());

    if($("#datepicker2").val() < $("#datepicker1").val()){
        alert("End Date must come after Start Date!");
    } 
});

这是一个小提琴:JSFiddle

在 MVC 中使用控制器的替代解决方案

一旦您将此页面提交给控制器,您就可以像这样 (C#) 进行检查:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NameOfAction(ViewModel nameOfViewModel)
{
    if(nameofViewModel.FinishDate <= nameofViewModel.StartDate)
    {
        ModelState.AddModelError("FinishDate", "Finish Date needs to be after the Start Date!");
        return View(nameOfViewModel);
    }
}

让我知道这是否有帮助,或者是否需要更改。

【讨论】:

  • 替代解决方案起到了作用,我会在更新问题时稍作调整。如果我稍微调整一下,JS 解决方案可能会起作用。 $("#id").val() 在我的情况下不起作用,可能是由于我的日期值的格式(dd/MM/yyyy)。
  • @Lernas 很高兴我能帮上忙!请将答案标记为已接受,以便可以将此问题视为已回答。
【解决方案2】:

根据我接受的 BviLLe_Kid 回答,这就是我最终要做的:

    if (ModelState.IsValid)
    {
        if (nameofViewModel.FinishDate <= nameofViewModel.StartDate)
        {
            ModelState.AddModelError(string.Empty, "Error message here");
            return View(nameofViewModel);
        }
        else {
            //code for when validation is OK
        }
    }

在视图中:

<div style="color:red;">
    @Html.ValidationSummary(true)
    @Html.ValidationMessageFor(model => model.FinishDate)
</div>

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2015-04-19
    • 2020-02-13
    相关资源
    最近更新 更多