Example of Date Range Picker For in ASP.NET MVC DateRangePicker Control
Click/Touch the submit button to post the selected value in the DateRangePicker. When posting the null value, validation error message will be shown below the DateRangePicker.
@Html.UiDateRangePickerFor(model => model.StartDate, model => model.EndDate, new UiDateRangePickerOptions { Width = "200px" })
@Html.UiButton(new UiButtonOptions { Id = "btn", Text = "Post", Type = UiButtonType.Submit, Color = UiButtonColor.Primary })
<style>
#wrapper { max-width: 246px; margin: 30px auto; padding-top: 50px; }
#submitbutton { margin-top: 20px; margin-left: 65px; }
.field-validation-error { color: red; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
@model CalendarsDemoController.DateRangePickerModel
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="control-section">
<div id="wrapper">
@Html.UiDateRangePickerFor(model => model.StartDate, model => model.EndDate, new UiDateRangePickerOptions { Width = "200px" })
<div>
@Html.ValidationMessageFor(model => model.StartDate)
@Html.ValidationMessageFor(model => model.EndDate)
</div>
<div id="submitbutton">
@Html.UiButton(new UiButtonOptions { Id = "btn", Text = "Post", Type = UiButtonType.Submit, Color = UiButtonColor.Primary })
</div>
</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/DateRangePickerFor.cshtml; it draws what ViewData holds.
public ActionResult DateRangePickerFor()
{
var model = new DateRangePickerModel();
model.StartDate = new DateTime(2020, 05, 23);
model.EndDate = new DateTime(2020, 06, 03);
return View(model);
}
[HttpPost]
public ActionResult DateRangePickerFor(DateRangePickerModel model)
{
// the posted value is obtained from the model
return View(model);
}
public class DateRangePickerModel
{
[Required(ErrorMessage = "Value is required")]
public DateTime? StartDate { get; set; }
[Required(ErrorMessage = "Value is required")]
public DateTime? EndDate { get; set; }
}
}
}
@* 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>
This sample demonstrates the strongly typed extension support in DateRangePicker. Value is set as model object to the DateRangePicker from the code behind, and its set to DateRangePicker in view page by invoking the strongly typed DateTimePickerFor helper with the lamba expression.