New to Telerik UI for ASP.NET Core? Start a free 30-day trial
DateInput in Razor Pages
Razor Pages is an alternative to the MVC pattern that makes page-focused coding easier and more productive. This approach consists of a cshtml
file and a cshtml.cs
file (by design, the two files have the same name).
You can seamlessly integrate the Telerik UI DateInput for ASP.NET Core in Razor Pages applications.
This article describes how to configure the DateInput component in a Razor Pages scenario.
For the complete project, refer to the DateInput in Razor Pages example.
csthml
@page
@model Telerik.Examples.RazorPages.Pages.DateInput.DateInputGlobalizationModel
@{
ViewData["Title"] = "DateInputGlobalization";
}
@using System.Globalization
@{
// Set the server culture.
var culture = CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de-DE");
}
/* Add the culture script from your local folder or the Kendo CDN. Use the culture variable from above to make sure the server and client cultures match. */
<script src="@Url.Content("https://kendo.cdn.telerik.com/2020.3.1118/js/cultures/kendo.culture." + culture + ".min.js")"></script>
<script type="text/javascript">
// Set the client culture.
kendo.culture("@culture");
</script>
<div>
<h4>Enter a date</h4>
@(Html.Kendo().DateInput()
.Name("dateinput")
.Value(DateTime.Today)
)
@(Html.Kendo().DateInput()
.Name("dateinput2")
)
</div>
<style>
div {
text-align: center;
}
</style>
Binding the DateInput to a PageModel Property
To bind the DateInput to a property from the PageModel
, follow the next steps:
-
Add a property to the
PageModel
that must bind to the DateInput.Index.cshtml.cspublic class IndexModel : PageModel { [BindProperty] public DateTime DateCreated { get; set; } public void OnGet() { DateCreated = DateTime.Now; // Assign a value to the "DateCreated" property, if needed. } }
-
Declare the
PageModel
at the top of the page.C#@page @model IndexModel
-
Bind the DateInput to the property using the
DateInputFor()
configuration.HtmlHelper_Index.cshtml@page @model IndexModel @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() @(Html.Kendo().DateInputFor(m => m.DateCreated))