0

I am trying to make an asp.net mvc view for a user to use the jQuery UI Datepicker to select only from a specific number of available dates like the picture below.

a busy cat

So far I have used the controller to pass in a List of Datepicker to the view, which includes all of the dates that the jQuery Calendar should show to the user. The problem is, the calendar remains blank for some reason i.e. the dates from the database are not showing up in the calendar.

My Datepicker Model:

namespace BookingSys.Models
{
  public class Datepicker
  {
    public DateTime Date { get; set; }
    public int Id { get; set; }
    public int LecturerId { get; set; }
    public string Comment { get; set; }
  }
}

DbContext:

namespace BookingSys.Models
{
  public class BookingSysDb : DbContext
  {
    public DbSet<Datepicker> Dates { get; set; }
  }
}

My Booking Controller:

[HttpGet]
    public ActionResult Booking()
    {
        var model = db.Dates.ToList();
        return View(model);
    }

My Schoool/Booking View :

@model List<BookingSys.Models.Datepicker>

@{
ViewBag.Title = "Booking";
}

<h2>School Bookings</h2>

<form asp-controller="School" asp-action="Booking" method="post">
<input asp-for="School.Date" id="txtDate" /><br />
<button>Submit</button>
</form>

<link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />

@section scripts {

<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
<script>
            jQuery(function () {
                var enableDays = [' @(string.Join("','", Model.Select(d => 
               d.Date.ToString("dd-MM-yyyy")))) ']; // **** I think this line of code may be causing the problem

                function enableAllTheseDays(date) {
                    var sdate = $.datepicker.formatDate('dd-mm-yy', date)
                    console.log(sdate)
                    if ($.inArray(sdate, enableDays) != -1) {
                        return [true];
                    }
                    return [false];
                }

                $("#txtDate").datepicker({
                    dateFormat: "dd/MM/yy",
                    beforeShowDay: enableAllTheseDays

                });
            });
  </script>

  @Scripts.Render("~/bundles/jqueryval")
}

<style>
.my-class a {
    background-color: #07ea69 !important;
    background-image: none !important;
    color: #ffffff !important;
}
</style>

Can anyone tell me why my calendar is not showing the dates from the Datepicker database conext? Even though the Datepicker database holds 10 dates in the month of July 2019, they are not showing up in the calendar the view.

Cheers

3
  • Consider adding console.log(enableDays); after it's been created and examine the output. Make sure you're getting the data you expect in HTML/JavaScript.
    – Twisty
    Commented Jul 11, 2019 at 1:42
  • Also I would return return [true, "", ""]; or return [false, "", ""]; as described in the API: api.jqueryui.com/datepicker/#option-beforeShowDay Index 0 and 1 are required, 2 is Optional.
    – Twisty
    Commented Jul 11, 2019 at 1:47
  • @Twisty Thanks for the reply mate! I added console.log and got the following returned: Array(1) 0: " 10-10-1990&#39;,&#39;07-01-2019&#39;,&#39;07-31-2019&#39;,&#39;07-15-2019 " length: 1. Can you see why this line of code is giving this output? My JavaScript knowledge isn't the best
    – Craig
    Commented Jul 11, 2019 at 11:28

3 Answers 3

0

Try the solution here.

https://forum.jquery.com/topic/default-date-for-date-picker

It looks like you need to use the setDate command. That may do what you need.

1
  • Thanks for the reply. Although I think you misunderstood my question. I have no problem getting the date into the field from the Calendar UI, the problem I have is even though the Datepicker database holds 10 dates in the month of July 2019, they are not showing up in the calendar in the view.
    – Craig
    Commented Jul 10, 2019 at 18:49
0

So apparently, it's encoding it to prevent code injection as a defense against XSS. Since this is known to be just DateTime values, It can be overridden using @Html.Raw

var enableDays = ['@Html.Raw(string.Join("','", Model.Select(d => 
d.DtmDate.ToString("MM-dd-yyyy")))'];
0

Ok, try this solution:

https://forum.jquery.com/topic/jquery-datepicker-pick-multiple-dates

Based on that, you will either need a 3rd party plug-in, or you'll have to "roll your own".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.