不允许用户选择现有日期

时间:2012-12-10 16:39:03

标签: c# asp.net-mvc html-helper

因此,用户从两个日历中选择假期(开始日期和结束日期),然后从开始到结束的日期将在每个日期的单独记录中存储在数据库中。

(感谢前一个问题的帮助,一切正常)

现在,我想要某种错误消息,如果用户选择的日期已存在于数据库中,则不允许单击该按钮。

我不确定这是从视图还是控制器完成的?

查看:

<form action ="ListHolidays" id="listHolidays" method="post">
@using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
    <fieldset>
        <legend>Holiday</legend>

        <div>
            @Html.LabelFor(model => model.PersonId, "Person")
        </div>

        <div>     
            @Html.DropDownListFor(model => model.PersonId,
                                new SelectList(ViewBag.Id, "Value", "Text"),
                                "---Select---"
                                )   
         @Html.ValidationMessageFor(model => model.PersonId)            
        </div>

        <div>
            @Html.LabelFor(model => model.HolidayDate)
        </div>

        <div>

            @Html.TextBoxFor(model => model.HolidayDate)

            @Html.TextBoxFor(model => model.endDate)
    <script>

//        Date.format = 'dd/m/yyy';
        $("#HolidayDate").addClass('date-pick');
        $("#endDate").addClass('date-pick');
            //$('.date-pick').datePicker//({dateFormat: 'dd-mm-yy'}).val();

//        clickInput: true

        $(function () {
        //3 methods below dont allow user to select weekends
            $('.date-pick').datePicker(
               {
                   createButton: false,
                   renderCallback: function ($td, thisDate, month, year) 
                   {
                       if (thisDate.isWeekend()) 
                       {
                           $td.addClass('weekend');
                           $td.addClass('disabled');
                       }
                   }
               }
        )

        .bind('click',
            function () 
            {
                $(this).dpDisplay();
                this.blur();
                return false;
            }
        )

        .bind('dateSelected',
            function (e, selectedDate, $td) 
            {
                console.log('You selected ' + selectedDate);
            }
        );

//        HolidayDate is start date
            $('#HolidayDate').bind('dpClosed',
                    function (e, selectedDates) 
                    {
                        var d = selectedDates[0];
                        if (d) 
                        {
                            d = new Date(d);
                            $('#endDate').dpSetStartDate(d.addDays(1).asString());
                        }
                    }
            );

            //end date is end date
            $('#endDate').bind('dpClosed',
                    function (e, selectedDates) 
                    {
                        var d = selectedDates[0];
                        if (d) 
                        {
                            d = new Date(d);
                            $('#HolidayDate').dpSetEndDate(d.addDays(-1).asString());
                        }
                    }
                );
        });



    </script>

     @Html.ValidationMessageFor(model => model.HolidayDate)
        </div>

        <p>
            <input type="submit" value="Create"/>
        </p>

控制器:

  [HttpPost]
    public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate)
    {

            DateTime startDates = Convert.ToDateTime(HolidayDate),
                     endDates = Convert.ToDateTime(endDate);

            while (startDates <= endDates)
            {
                if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
                {
                    Holiday holiday1 = new Holiday();
                    holiday1.PersonId = PersonId.Value;
                    holiday1.HolidayDate = startDates;

                    db.Holidays.AddObject(holiday1);
                    db.SaveChanges();



                    //say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
                    //but doesnt chage the value of startdates = 'startdates'
                }

                startDates = startDates.AddDays(1);
            }

            return RedirectToAction("Index");
        }

如果holidayDate = db.exisitng date?

不确定如何解决这个问题。

请告知。

1 个答案:

答案 0 :(得分:1)

根据您的需要,您可以选择几个选项:

  1. 将现有日期预加载到某种类型的JS数组/集合中,然后在更改值时对该数组进行验证。
  2. 将ajax查询附加到datepicker的已更改事件。当用户更改日期时,请执行查询并验证该日期。

         $('.date-pick').bind('dateSelected',function (e, selectedDate, $td){
        $.ajax({
           url:'somepath/action',
           data: {selectedDateVal: selectedDate},
           success:function(result){
                if(result == true){
                  //value exists
                 }else{
                   //value is good
                 }
           }
        });}); ​
    
  3. 查看自定义数据注释。希望我能就此提供更多意见,但不幸的是我在这方面没有太多经验。