Example of Date Range in ASP.NET MVC Calendar Control
In the following sample, a specific date ranges from 5th to 27th of a month has been set to select from the Calendar. All the other dates are out of range and restricted to set or select.
Selected Value:
@Html.UiCalendar(new UiCalendarOptions { Id = "calendar", Min = (DateTime)ViewData["minDate"], Max = (DateTime)ViewData["maxDate"], OnChange = "startDate" })
<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; }
</style>
<div class="control_wrapper">
<div id="container" style="overflow:auto">
@Html.UiCalendar(new UiCalendarOptions { Id = "calendar", Min = (DateTime)ViewData["minDate"], Max = (DateTime)ViewData["maxDate"], OnChange = "startDate" })
<br />
<span id="date_label"> Selected Value: </span>
</div>
</div>
<script>
function startDate(value) {
document.getElementById('date_label').textContent = 'Selected Value: ' + value.toLocaleDateString();
}
</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/DateRange.cshtml; it draws what ViewData holds.
public ActionResult DateRange()
{
ViewData["minDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 05);
ViewData["maxDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 27);
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>
Date Range sample illustrates the date selection within a specific range in a calendar by using the min and max properties. Here, the date selection range was resricted within 5th to 27th of this month.