Example of Disabled Dates in ASP.NET MVC DatePicker Control
In the following sample, all the weekends (Saturday and Sunday) of a month are disabled, and these dates are restricted to set or select in the DatePicker.
@Html.UiDatePicker("datepicker", new UiDatePickerOptions { OnRenderDayCell = "disableDate", Placeholder = "Choose a date" })
<style>
#wrapper { max-width: 246px; margin: 30px auto; padding-top: 50px; }
</style>
<div class="control-section">
<div id="wrapper">
@Html.UiDatePicker("datepicker", new UiDatePickerOptions { OnRenderDayCell = "disableDate", Placeholder = "Choose a date" })
</div>
</div>
<script>
// Syncfusion: the weekends are disabled.
function disableDate(args) {
if (args.date.getDay() === 0 || args.date.getDay() === 6) {
args.isDisabled = true;
}
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DatePickerController : Controller
{
// The page is Views/DatePicker/DisabledDates.cshtml; it draws what ViewData holds.
public ActionResult DisabledDates()
{
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>
Disabled Dates sample demonstrates how to disable specific dates in the DatePicker by using renderDayCell event. This event gets triggered on each day cell element creation, that allows you to customize, or disable specific dates in the DatePicker. Here the weekend dates are disabled by using renderDayCell.