New to Telerik UI for ASP.NET CoreStart a free 30-day trial

DateTimePicker 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 DateTimePicker for ASP.NET Core in Razor Pages applications.

This article describes how to configure the DateTimePicker component in a Razor Pages scenario.

For the complete project, refer to the DateTimePicker in Razor Pages example.

cshtml
@page

<div>
    <h4>Select a date range</h4>
    @(Html.Kendo().DatePicker()
        .Name("datepicker")
    )
</div>

Binding the DateTimePicker to a PageModel Property

To bind the DatePicker to a property from the PageModel, follow the next steps:

  1. Add a property to the PageModel that must bind to the DateTimePicker.

    Index.cshtml.cs
        public class IndexModel : PageModel
        {
            [BindProperty]
            public DateTime DateCreated { get; set; }
    
            public void OnGet()
            {
                DateCreated = DateTime.Now; // Assign value to the "DateCreated" property, if needed.
            }
        }
  2. Declare the PageModel at the top of the page.

    C#
        @page
        @model IndexModel
  3. Bind the DateTimePicker to the property using the DateTimePickerFor() configuration.

    HtmlHelper_Index.cshtml
        @page
        @model IndexModel
    
        @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
        @Html.AntiForgeryToken()
        
        @(Html.Kendo().DateTimePickerFor(m => m.DateCreated))

See Also