Search results

Render DateRangePickerFor in ASP.NET MVC DateRangePicker control

26 Nov 2021 / 1 minute to read

The DateRangePickerFor component can be rendered by passing value from the model. The selected date range value can be retrieved during form submission using the post method at the server end.

The following sample demonstrates how to retrieve the value in the controller by rendering the DateRangePickerFor component.

razor
daterangepickerfor.cs
Copied to clipboard
@model EJ2CoreSampleBrowser.Controllers.DateRangePicker
@using (Html.BeginForm())
{
    @Html.EJS().DateRangePickerFor(model => model.value).Render()
    <div>
        @Html.ValidationMessageFor(model => model.value)
    </div>
    <div id="submitbutton">
        @Html.EJS().Button("btn").Content("Post").Render()
    </div>
}
Copied to clipboard
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace EJ2CoreSampleBrowser.Controllers
{
    public class DateRangePicker
    {
        [Required(ErrorMessage = "Please enter the value")]
        public DateTime?[] value { get; set; }
    }
    public class HomeController : Controller
    {
        DateRangePicker DateRangeValue = new DateRangePicker();
        public ActionResult Index()
        {
            DateRangeValue.value = new DateTime?[] { new DateTime(2020, 03, 03), new DateTime(2021, 09, 03) };
            return View(DateRangeValue);
        }
        [HttpPost]
        public ActionResult Index(DateRangePicker model)
        {
            //posted value is obtained from the model
            DateRangeValue.value = model.value;
            var stratDate = model.value[0];
            var endDate = model.value[1];
            return View(DateRangeValue);
        }
    }
}

Selected value will be retrieved as below.

DateRangePickerFor Component in ASP.NET MVC