1 Replies - 5594 Views - Last Post: 11 December 2012 - 02:54 AM

#1 Xna4life  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

dont allow user to select existing date

Posted 10 December 2012 - 09:42 AM

So the user selects holidays (start and end dates) from two calendars, the dates from start to end are then stored in the DB in separate records per date.

(thanks to help from a previous question this all works fine)

Now, I want some sort of error message which doesnt allow the button to be clicked if the dates the user are selecting already exist in the DB.

Im unsure if this is done from the view, or controller?

View:
<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>



controller:

[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");



so...? if holidayDate = db.exisitng date?

Unsure how to go about this.

Please advise.

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: dont allow user to select existing date

#2 Xna4life  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

Re: dont allow user to select existing date

Posted 11 December 2012 - 02:54 AM

any idea guys?

Thanks
J
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1