Example of Preset Ranges in ASP.NET MVC DateRangePicker Control
Click/Touch the DateRangePicker popup icon to view and select the list of custom preset ranges. Select the custom range option which is provided at the end of this list to open date range picker popup calendar for selecting custom ranges.
@Html.UiDateRangePicker("daterangepicker_start", "daterangepicker_end", new UiDateRangePickerOptions
{
Id = "daterangepicker",
Placeholder = "Choose a Range",
Presets = new List<UiDateRangePreset>
{
new UiDateRangePreset("This Week", (DateTime)ViewData["weekStart"], (DateTime)ViewData["weekEnd"]),
new UiDateRangePreset("This Month", (DateTime)ViewData["monthStart"], (DateTime)ViewData["monthEnd"]),
new UiDateRangePreset("Last Month", (DateTime)ViewData["lastMonthStart"], (DateTime)ViewData["lastMonthEnd"]),
new UiDateRangePreset("Last Year", (DateTime)ViewData["lastYearStart"], (DateTime)ViewData["lastYearEnd"])
}
})
<style>
#wrapper { max-width: 246px; margin: 30px auto; padding-top: 50px; }
</style>
<div class="control-section">
<div id="wrapper">
@Html.UiDateRangePicker("daterangepicker_start", "daterangepicker_end", new UiDateRangePickerOptions
{
Id = "daterangepicker",
Placeholder = "Choose a Range",
Presets = new List<UiDateRangePreset>
{
new UiDateRangePreset("This Week", (DateTime)ViewData["weekStart"], (DateTime)ViewData["weekEnd"]),
new UiDateRangePreset("This Month", (DateTime)ViewData["monthStart"], (DateTime)ViewData["monthEnd"]),
new UiDateRangePreset("Last Month", (DateTime)ViewData["lastMonthStart"], (DateTime)ViewData["lastMonthEnd"]),
new UiDateRangePreset("Last Year", (DateTime)ViewData["lastYearStart"], (DateTime)ViewData["lastYearEnd"])
}
})
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DateRangePickerController : Controller
{
// The page is Views/DateRangePicker/Presets.cshtml; it draws what ViewData holds.
public ActionResult Presets()
{
int days = (int)DateTime.Now.DayOfWeek;
DateTime lastMonth = DateTime.Now.AddMonths(-1);
ViewData["weekStart"] = DateTime.Now.AddDays(-days);
ViewData["weekEnd"] = ((DateTime)ViewData["weekStart"]).AddDays(6);
ViewData["monthStart"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
ViewData["monthEnd"] = ((DateTime)ViewData["monthStart"]).AddMonths(1).AddDays(-1);
ViewData["lastMonthStart"] = new DateTime(lastMonth.Year, lastMonth.Month, 1);
ViewData["lastMonthEnd"] = ((DateTime)ViewData["lastMonthStart"]).AddMonths(1).AddDays(-1);
ViewData["lastYearStart"] = new DateTime(DateTime.Now.Year - 1, 1, 1);
ViewData["lastYearEnd"] = new DateTime(DateTime.Now.Year - 1, 12, 31);
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>
DateRangePicker component has presets support to display the collection of required ranges in the popup element. User can select a required range from the list and the selected range value will be updated in the component.