Using Telerik's KendoUI DatePicker in an asp.NET MVC project which is also using Entity Framework. I'm adding an enhancement to an existing project to highlight dates on the DatePicker when a record for that date exists in the Oracle database.
In the View:
@{
var dateList = HelperMethodClass.GetRecordDates(model);
var dateString = dateList.Aggregate("", (current, date) => current + (date.Equals(dateList.Last()) ? date.ToString("yyyy-MM-dd") : date.ToString("yyyy-MM-dd") + ","));
}
GetRecordDates() is a method in a HelperMethodClass that queries the database for all the dates with a record for the model being passed. I format it so I can later control the parsing (see below). Later in a table in the same view, the DatePicker is placed in the table and I store the dates variable to pull in a different .cshtml page (which is under the View -> Shared -> EditorTemplates folder of the project):
<div class="leftActions">
<input type="hidden" id="myDates" value="@dateString" />
<table>
<tr>
<td>@Html.EditorFor(m => m.Date, new {@sourcePage = "MyView"})</td>
<tr>
</table>
</div>
Here's the other .cshtml page:
@model DateTime?
@{
var calendar = Html.Kendo().DatePickerFor(m => m).Format("yyyy-MM-dd");
if (ViewData["sourcePage"] == "MyView")
{
calendar.Footer("<p>" + DateTime.Today.ToLongDateString() + "</p><span id=\"legend\">Record Exists<span>");
calendar.MonthTemplate("# if ($.inArray(+data.date, recordDates) != -1) { #" + "<div class=\"reportDate\">" +
"</div>" + "# } #" + "#= data.value #");
}
}
<span style="width: 6em">
@calendar
</span>
<script>
var dates = $('#myDates');
var dateArray = dates.toString().split(',');
var recordDates = d(dateArray);
function d(x) {
var myArray = [];
for (var i = 0; i < x.length; i++) {
myArray.push(new Date(x[i].substring(0, 4), x[i].substring(5, 7), x[i].substring(8)));
}
return myArray;
};
</script>
<style>
#legend, .reportDate {
background-color:rgba(0,0,0,.2);
}
.reportDate {
position: absolute;
width: 32px;
height: 28px;
}
</style>
I believe my issue is when I get to the EditorTemplate file (where the DatePicker is actually created). I put a break-point at my code-behind GetRecordDates() method to see what was happening. The View variables are working as expected, and the dates variable in the EditorTemplate is pulling the date string from the View as expected, but that's where it stops. I tried putting a
in the View and use a .innerHtml in the EditorTemplate to set the text of the
equal to either the dateArray or recordDates variables, but, with debugging enabled on the browser, it shows an error that those two variables are null. The calendar shows up fine with the footer, but the dates aren't being highlighted as expected. If I hard-code the date array into the JavaScript, it highlights the dates. I've also tried passing the list created by the query directly into my dates variable, but the result is the same.
I'm new to web app development, so I imagine the issue can be fixed if I understood how the code is interpreted and run when the page is accessed. Any ideas on how I can highlight the dates from an array being created in the code-behind?