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

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

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

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

cshtml
@page
@model Telerik.Examples.RazorPages.Pages.DateRangePicker.DateRangePickerDefaultValueModel
@{
    ViewData["Title"] = "DateRangePickerDefaultValue";
}

<div>
    <h4>Select a date range</h4>
    @(Html.Kendo().DateRangePicker()
        .Name("daterangepicker")
        .Range(r => r.Start(DateTime.Now).End(DateTime.Now.AddDays(10))) // Add a default value using the Range method.
        .HtmlAttributes(new { style = "width: 100%", title = "daterangepicker" })
    )
</div>

<style>
    div {
        text-align: center;
    }
</style>

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 StartDate { get; set; }
    
            [BindProperty]
            public DateTime EndDate { get; set; }
    
            public void OnGet()
            {
                StartDate = DateTime.Now; // Assign values to the "StartDate" and "EndDate" properties, if needed.
                EndDate = DateTime.Now.AddDays(3);
            }
        }
  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().DateRangePickerFor(m => m.StartDate, m => m.EndDate)
            .Name("Dates")
            .Range(r => r.Start(Model.StartDate).End(Model.EndDate))
        )

See Also