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