Example of DateTimePickerFor in ASP.NET MVC DateTimePicker Control
Click/Touch the submit button to post the selected value in the DateTimePicker. When posting the null value, validation error message will be shown below the DateTimePicker.
@Html.UiDateTimePickerFor(model => model.objectValue, new UiDateTimePickerOptions { 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.DateTimePickerModel
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="control-section">
<div id="wrapper">
@Html.UiDateTimePickerFor(model => model.objectValue, new UiDateTimePickerOptions { Width = "200px" })
<div>
@Html.ValidationMessageFor(model => model.objectValue)
</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 DateTimePickerController : Controller
{
// The page is Views/DateTimePicker/DateTimePickerFor.cshtml; it draws what ViewData holds.
public ActionResult DateTimePickerFor()
{
var model = new DateTimePickerModel();
model.objectValue = DateTime.Now;
return View(model);
}
[HttpPost]
public ActionResult DateTimePickerFor(DateTimePickerModel model)
{
// the posted value is obtained from the model
return View(model);
}
public class DateTimePickerModel
{
[Required(ErrorMessage = "Value is required")]
public DateTime? objectValue { 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 DateTimePicker. Value is set as model object to the DateTimePicker from the code behind, and its set to DateTimePicker in view page by invoking the strongly typed DateTimePickerFor helper with the lamba expression.