--------------------------------------------------------------------------------------------------------
So the Controller looks like this;
[HttpGet]
public ActionResult Get()
{
return BankHolidayViewModel.GetAllBankHolidays();
}
---------------------------------------------------------------------------------------------------------
The Model looks like this;
public static JsonResult GetAllBankHolidays()
{
using (SHPContainerEntities db = new SHPContainerEntities())
{
return ReturnJSON(db.BankHolidays.Select(x => x.BankHolidayDate).ToArray<DateTime>());
}
}
private static JsonResult ReturnJSON(DateTime[] bankholidays)
{
JsonResult result = new JsonResult();
result.Data = bankholidays;
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
}
------------------------------------------------------------------------------------------------------------------
My AJAX and Jquery is the part that doesn't work. I checked on Firebug for what is going on. The dates are returned from the server OK, but I do not know how to iterate through the dates collection.
<script type="text/javascript">
$(document).ready(function () {
var holiDays = (function () {
var val = null;
$.getJSON('/BankHoliday/Get', function (data) {
val = data;
});
return val;
})();
function nationalDays(date) {
var m = date.getMonth();
var d = date.getDate();
var y = date.getFullYear();
for (var i = 0; i < holiDays.length - 1; i++) {
var myDate = new Date(holiDays[i]);
if ((m == (myDate.getMonth())) && (d == (myDate.getDate())) && (y == (myDate.getFullYear()))) {
return [false];
}
}
return [true];
}
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
// noWeekendsOrHolidays
// $.datepicker.noWeekends
$(function () {
$(".datePicker").datepicker({ showOn: 'both', dateFormat: 'dd MM yy', changeMonth: true, changeYear: true, yearRange: 'c-1:c+1', beforeShowDay: noWeekendsOrHolidays });
});
});
</script>