使用bootstrap.datepicker进行C#MVC模型远程验证

时间:2018-11-29 03:23:36

标签: c# asp.net-mvc twitter-bootstrap-3 model datepicker

我在C#MVC项目的模型中使用了远程功能进行验证。 请在下面找到代码:

public class ProjectModel {
    public string StartDateFormatted { get; set; }
    [Remote(action: "CheckEndDate", controller: "Project", AdditionalFields = "StartDateFormatted")]
    public string EndDateFormatted { get; set; }
}

对于EndDate的验证总是晚于StartDate。 请在下面找到代码:

public class ProjectController : Controller
{
        [AcceptVerbs("Get", "Post")]
        public ActionResult CheckEndDate(ProjectModel project)
        {
            if (project.EndDateFormatted != null && project.StartDateFormatted != null)
            {
                if (DateTime.Parse(project.StartDateFormatted) > DateTime.Parse(project.EndDateFormatted))
                {
                    return Json("<p><span style='vertical-align: 2px'>Entered Date later than Start Date </span></p>", JsonRequestBehavior.AllowGet);
                }
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }
}

该视图有两个输入StartDate和EndDate。我已经使用bootstrap.datepicker(https://github.com/uxsolutions/bootstrap-datepicker)作为我的日期选择器,以便在视图中选择dd-M-yyyy格式的日期。

$(document).ready(function () {
    try {
        $('input.datepicker').datepicker({
                format: "dd-M-yyyy",
                maxViewMode: 0,
                todayBtn: "linked",
                clearBtn: true,
                autoclose: true,
                todayHighlight: true
            });
    }
    catch (e) {
        console.log(e.message);
    }
});
/*!
 * Datepicker for Bootstrap v1.8.0 (https://github.com/uxsolutions/bootstrap-datepicker)
 *
 * Licensed under the Apache License v2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 */
<div class="col-6" style="padding-top:10px;padding-bottom:10px">
            <div class="col-11">
                <div class="col-12">
                    @Html.LabelFor(model => model.StartDateFormatted, htmlAttributes: new { @class = "" })
                </div>
                <div class="col-12">
                    @Html.EditorFor(model => model.StartDateFormatted, new { htmlAttributes = new { @class = "form-control datepicker newinput", @id = "StartDate", @autocomplete = "off" } })
                    <br />
                    @Html.ValidationMessageFor(model => model.StartDateFormatted, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="col-6" style="padding-top:10px;padding-bottom:10px">
            <div class="col-11">
                <div class="col-12">
                    @Html.LabelFor(model => model.EndDateFormatted, htmlAttributes: new { @class = "" })
                </div>
                <div class="col-12">
                    @Html.EditorFor(model => model.EndDateFormatted, new { htmlAttributes = new { @class = "form-control datepicker newinput", @id = "EndDate", @autocomplete = "off" } })
                    <br />
                    @Html.ValidationMessageFor(model => model.EndDateFormatted, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

Project Controller中验证功能CheckEndDate的值是先前存储在EndDate输入文本框中的值。 我的假设是,值更改实际上是在其实际更改值之前由引导日期选择器触发的。所以,这就是为什么验证功能要获取先前的值而不是更改后的值

0 个答案:

没有答案