0

I am trying to make an appointment page in MVC4. It is working well but I would like to disable the date which are chosen before.So the rule is just one appointment per day. Here is my controller to make an appointment:

 public ActionResult Make()
           {
               return View();
           }
           [HttpPost]
           [ValidateAntiForgeryToken]
           public ActionResult Make(Models.AppModel User)
           {
               if (Session["UserEmail"] != null)
               {
                   using (var db = new MaindbModelDataContext())
                   {
                       var patient = db.Patients.FirstOrDefault(u => u.Email == (String)Session["UserEmail"]);

                       var app = new Appointment();

                       app.Date = (DateTime)User.Date;
                       app.Description = User.Description;
                       app.Status = "isPending";
                       app.PatientNo = patient.PatientNo;
                       app.AppNo = Guid.NewGuid().GetHashCode();
                       db.Appointments.InsertOnSubmit(app);
                       db.SubmitChanges();

                   }
               }
               else
               {
                   return RedirectToAction("Index", "User");
               }
               return RedirectToAction("Index", "Patient");
           }
           //
       }
    }

and here is my view with the datepicker

@model DentAppSys.Models.AppModel
@{
    ViewBag.Title = "Appointment";
}
<link type="text/css" rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken();
    @Html.ValidationSummary(true, "");
    <div>

        <fieldset>
            <legend>Get an Appointment</legend>


            <div>@Html.LabelFor(u => u.Date)</div>
            <div>
                @Html.TextBoxFor(u => u.Date, htmlAttributes: new { id = "DatePicker" })
                @Html.ValidationMessageFor(u => u.Date)

            </div>

            <div>@Html.LabelFor(u => u.Description)</div>
            <div>
                @Html.TextBoxFor(u => u.Description)

            </div>

            <input type="submit" value="Submit" class="footer_btn" />

        </fieldset>

    </div>
}
<script type="text/javascript">
    $(function () {
        $("#DatePicker").datepicker();
    });

</script>
user1953051
  • 321
  • 2
  • 7
  • 21

1 Answers1

0

It is not clear what dates are to be disabled. Either way, I would suggesting making an Ajax GET request through jQuery and extracting those dates (EF query to get those dates) and storing them in a javascript array. You can then use this array as the "beforeShowDay" parameter for the datepicker, as answered in this StackOverFlow Question.

EDIT after comments :

Add the following action to your controller :

 public JsonResult GetReservedDates()
        {
            var db = new MaindbModelDataContext();
            var dates = db.Appointment.ToList().Select(x => x.Date);

            return Json(new { dates }, "text/x-json", JsonRequestBehavior.AllowGet);
        }

Then, make an ajax call to it on your page :

$.get("/ControllerName/GetReservedDates",function(data){
var reserveddates = data.dates;
$('#DatePicker').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [ reserveddates.indexOf(string) == -1 ]
    }
});

});

You may have to format the dates from c# to javascript. It depends on the datatype you have.

Hope this helps.

Community
  • 1
  • 1
thispatchofsky
  • 854
  • 6
  • 15