MVC4中的开始日期和结束日期之间的日期验证

时间:2014-08-19 08:42:34

标签: asp.net-mvc-4 validation date range

我正在执行一个程序来过滤两个日期之间的数据,即开始日期和结束日期。在我的控制器中我有这个:

 public ActionResult List(DateTime startdate, DateTime enddate)
        {


            if (enddate < startdate)
            {

                return PartialView("DateValidation");
            }
            else
            {
                List<Card> cards = new List<Card>();

                using (var db = new CardDBEntities())
                {

                    var cardr = (from c in db.Cards
                                 join d in db.RegistrationDTs
                                 on c.CardId equals d.CardId
                                 where d.RegistrationDateTime >= startdate &&
                                       d.RegistrationDateTime <= enddate
                                 select new eReader1.Models.Model
                                 {
                                     CardId = d.CardId,
                                     Description = c.Description,
                                     RegistrationDateTime = d.RegistrationDateTime
                                 }).OrderByDescending(x => x.RegistrationDateTime).ToList();


                    //WebGrid grid = new WebGrid(cardr);

                    //ViewData["cardr"] = cardr;



                    return View(cardr);
                }

如您所见,视图取代了我的整个浏览器。我想将错误消息显示为接近日期框的字符串,或类似的内容。

我该怎么做?

先谢谢你

2 个答案:

答案 0 :(得分:0)

控制器:

 public ActionResult List(DateTime startdate, DateTime enddate) 
        {


                List<Card> cards = new List<Card>();

                using (var db = new CardDBEntities())
                {

                    var cardr = (from c in db.Cards
                                 join d in db.RegistrationDTs
                                 on c.CardId equals d.CardId
                                 where d.RegistrationDateTime >= startdate &&
                                       d.RegistrationDateTime <= enddate
                                 select new eReader1.Models.Model
                                 {
                                     CardId = d.CardId,
                                     Description = c.Description,
                                     RegistrationDateTime = d.RegistrationDateTime
                                 }).OrderByDescending(x => x.RegistrationDateTime).ToList();


                    //WebGrid grid = new WebGrid(cardr);

                    //ViewData["cardr"] = cardr;



                    return View(cardr);
                }

            }

观点:

link href="~/Content/themes/base/minified/jQuery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="~/Scripts/jquery-ui-1.8.2.min.js"></script>








@using (Html.BeginForm())
{
    <fieldset>
        <legend>Search criteria</legend>



        @Html.Label("StartDate", "Start Date:")  
         @Html.TextBox("startdate", String.Empty, new { @Class = "startdate" })
        @Html.Label("enddate", "End Date:")
        @Html.TextBox("enddate", String.Empty, new { @Class= "enddate" })



                <input type="submit" value="Apply" />
    </fieldset>
}

<script type="text/javascript">
    $(document).ready(function () {
        $(".startdate").datetimepicker({
            dateFormat: 'mm/dd/yy', timeFormat: 'hh:mm:ss',
            onSelect: function (selected) {
                $(".enddate").datepicker("option", "minDate", selected)
            }
        });

        $(".enddate").datetimepicker({

            dateFormat: 'mm/dd/yy', timeFormat: 'hh:mm:ss',
            onSelect: function (selected) {
                $(".startdate").datepicker("option", "minDate", selected)
            }
        });
    });
</script>

所以我在datetimepicker和其他jquery ui使用了jQuery插件:

<link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" type="text/css" />
<script src="jquery.min.js"></script>
<script src="jquery-ui.min.js"></script>
<script src="/Scripts/jquery-ui-timepicker-addon.js"></script>
<link href="~/Content/themes/base/minified/jQuery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="~/Scripts/jquery-ui-1.8.2.min.js"></script>

答案 1 :(得分:0)

你可以写这种功能。在此功能中,您需要在开始日期字段选择器中禁用日期之前的日期,然后显示。

$('#enddatep').click(function (e) {
e.preventDefault();
$('.enddate').datepicker('show');

});