Render DateRangePickerFor

28 Mar 20222 minutes 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.

@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>
}
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 startDate = model.value[0];
            var endDate = model.value[1];
            return View(DateRangeValue);
        }
    }
}

DateRangePickerFor Component in ASP.NET MVC