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

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

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

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

cshtml
@page

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

Binding the DatePicker 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 DatePicker.

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

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

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

See Also