Example of Special Dates in ASP.NET MVC Calendar Control
In the following sample, specific dates are highlighted. In desktop mode, highlighted information about the date will be displayed when hovered.
Selected Value:
@Html.UiCalendar(new UiCalendarOptions { Id = "calendar", OnRenderDayCell = "customDates", OnChange = "valueChange" })
<style>
#date_label { display: block; width: 248px; color: rgba(0, 0, 0, 0.58); margin-left: 5px; }
.control_wrapper { max-width: 300px; margin: 0 auto; }
.ui-theme-dark #date_label { color: white; }
.flatpickr-day.special { position: relative; }
.flatpickr-day.special span.highlight { position: absolute; left: 50%; bottom: 3px; width: 4px; height: 4px; margin-left: -2px; border-radius: 50%; background: rgb(0, 0, 255); }
.flatpickr-day.special.birthday span.highlight { background: #ff6161; }
#calender-control { max-width: 300px; margin: 0 auto; }
</style>
<div class="control_wrapper">
<div id="calender-control" style="overflow:auto">
@Html.UiCalendar(new UiCalendarOptions { Id = "calendar", OnRenderDayCell = "customDates", OnChange = "valueChange" })
<br />
<span id="date_label"> Selected Value: </span>
</div>
</div>
<script>
// Syncfusion: the 10th, the 15th and the 25th are special days with a title.
function customDates(args) {
if (args.date.getDate() === 10) { specialDate(args, 'Birthday'); }
if (args.date.getDate() === 15) { specialDate(args, 'Farewell'); }
if (args.date.getDate() === 25) { specialDate(args, 'Vacation'); }
}
function specialDate(args, name) {
var mark = document.createElement('span');
mark.className = 'highlight';
args.element.classList.add('special', name.toLowerCase());
args.element.title = name + '!';
args.element.setAttribute('data-val', name + '!');
args.element.appendChild(mark);
}
// The label also says what the selected day is.
function valueChange(value) {
var day = document.querySelector('#calendar .flatpickr-day.selected');
var title = day && day.getAttribute('data-val');
document.getElementById('date_label').textContent = 'Selected Value: ' + value.toLocaleDateString() + (title ? ' ( ' + title + ' )' : '');
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class CalendarController : Controller
{
// The page is Views/Calendar/SpecialDates.cshtml; it draws what ViewData holds.
public ActionResult SpecialDates()
{
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
Special Dates sample demonstrates, how to customize specific dates in a calendar by using the renderDayCell event. This event gets triggered on each day cell element creation that allows you to customize or disable the specific dates in calendar. Here 10, 15 and 20 date's are customized with custom styles and custom classes.