Search results

Render DateRangePickerFor

03 Dec 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.

tagHelper
daterangepickerfor.cs
Copied to clipboard
@model EJ2CoreSampleBrowser.Controllers.DateRangePicker
<form method="post">
    <ejs-daterangepicker id="daterangepickerFor" ejs-for="@Model.value"></ejs-daterangepicker>
    <div id="errorMessage">
        <span asp-validation-for="value"></span>
    </div>
    <div id="submitbutton">
        <ejs-button id="submitButton" content="Submit"></ejs-button>
    </div>
</form>
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 startDate = model.value[0];
            var endDate = model.value[1];
            return View(DateRangeValue);
        }
    }
}

Selected value will be retrieved as below.

DateRangePickerFor Component in ASP.NET MVC