如果选择了过去的日期,则ASP.NET Mvc警告消息 - c#

在我的mvc网络应用程序中,我有一个供员工用来提交假期请求的表格。如果选择了过去的日期,是否可以显示警告消息?类似于确认消息,但我仍然希望员工能够选择过去的日期。

这是我在视图中的表单:

     @using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal" style=" position:relative; top:20px;border-radius: 0px; border-color: #F47B20; border-style: solid; border-width: 5px; background-repeat: no-repeat; background-position: right; padding: 60px; background-size: contain; background-color:white ">
        <h2 align="center">Holiday Request Form</h2>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.StartDate, "Start Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StartDate, "Start Date", new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
                @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.FinishDate, "Finish Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FinishDate, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
                @Html.ValidationMessageFor(model => model.FinishDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HoursTaken, "Hours Requested", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HoursTaken, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HoursTaken, "", new { @class = "text-danger" })
            </div>
        </div>




        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Submit" class="btn btn-warning" />
            </div>
        </div>
    </div>
}

@section Scripts {
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/cssjqryUi")

<script type="text/javascript">

    $(document).ready(function () {
        $('input[type=datetime]').datepicker({
            dateFormat: "dd/M/yy",
            changeMonth: true,
            changeYear: true,
            yearRange: "-70:+70"
        });

    });
</script>

}

控制器:

[Authorize(Roles = "Admin,User,SuperUser")]
    public ActionResult Create()
    {
        ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FullName");
        return View();

        string name = Session["Name"].ToString();

        var EmployeeIDCatch = db.Employees.Where(s => s.Email.Equals(name)).Select(s => s.EmployeeID);

    }

    // POST: HolidayRequestForms/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "RequestID,StartDate,FinishDate,HoursTaken,Comments,YearCreated,MonthCreated,DayCreated,YearOfHoliday,Approved,SubmittedBy,ApprovedBy")] HolidayRequestForm holidayRequestForm)
    {
        if (ModelState.IsValid)
        {

            if (Session["Name"] == null)
            {
                TempData["msg"] = "Your Session Expired - Please Login";
                return RedirectToAction("Login", "Account");
            }

            string name = Session["Name"].ToString();

            var employeeID = db.Employees.Where(s => s.Email.Equals(name)).Select(s => s.EmployeeID).FirstOrDefault();
            holidayRequestForm.EmployeeID = employeeID;

            var submittedby = db.Employees.Where(s => s.Email.Equals(name)).Select(s => s.Email).FirstOrDefault();
            holidayRequestForm.SubmittedBy = submittedby;


            db.HolidayRequestForms.Add(holidayRequestForm);
            db.SaveChanges();
            SendMailToAreaManager();
            SendMailToManager();
            SendMailToAdmin();
            return RedirectToAction("Index", "Calendar");
        }

        ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FullName", holidayRequestForm.EmployeeID);
        return View(holidayRequestForm);
    }

参考方案

您可以使用jQuery完成所有工作。这是代码:

HTML:

<input placeholder="dd/mm/yyyy" id="datepicker"/>
<p id="warning" style="color:orange"></p>

jQuery的:

 $( function() {
    $( "#datepicker" ).datepicker({
        onSelect: function(date) {
          let dateNow = new Date();
          let dateSelect = new Date(date);
          dateNow.setHours(0,0,0,0);

          if(dateNow > dateSelect){            
            $("#warning").show().text("The date in the past is selected")
          }else{
            $("#warning").hide()
          }
       }
    });
});

将asp.net.core 2.0应用发布到IIS时JavaScript无法运行 - javascript

我是这里的新手。我有一个VS2017 asp.net C#Web应用程序。我目前不在使用Angular。这是一个简单的网站。我隐藏了div,当用户单击菜单项时,相关的div出现,而我隐藏了其余的div。我使用JavaScript完成此任务。在开发和IIS Express中,它可以工作。当我发布到IIS时,它不起作用。静态页面显示“确定”,但菜单按钮不执行任何…

使用Java脚本基于另一个控件项来验证ASP.NET C#控件 - c#

我正在尝试确保一个下拉框或另一个下拉框具有选定的条目,不能两者都选,并且不能都留空/未选择。每次单击运行javascript验证代码的搜索按钮时,即使我只是从一个下拉列表中选择,甚至从两个下拉列表中都没有选择,我都会收到一条消息,好像在两个下拉框中都选择了一样!这可能是我的逻辑,但是,我认为也可能是我的变量读为null。是否有人对问题可能是什么以及如何解决这…

ASP.net C#崩溃一行 - c#

我有一个母版页,在on load事件中包含以下几行: string menuIDdata = Page.Request.QueryString["mid"]; menuID = 0; // Get the menu ID if (!int.TryParse(menuIDdata, out menuID)) { menuID = 0; } …

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…