Search results

Render DatePickerFor in ASP.NET Core DatePicker control

08 Dec 2021 / 1 minute to read

The DatePickerFor component can be rendered by passing a value from the model. The selected date 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 DatePickerFor component.

tagHelper
datepickerfor.cs
Copied to clipboard
@model EJ2CoreSampleBrowser.Controllers.DatePicker
<form method="post">
    <ejs-datepicker id="datepickerFor" ejs-for="@Model.value"></ejs-datepicker>
    <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 DatePicker
    {
        [Required(ErrorMessage = "Please enter the value")]
        public DateTime? value { get; set; }
    }
    public class HomeController : Controller
    {
        DatePicker DatePickerValue = new DatePicker();
        public ActionResult Index()
        {
            DatePickerValue.value =  new DateTime(2020, 03, 03);
            return View(DatePickerValue);
        }
        [HttpPost]
        public ActionResult Index(DatePicker model)
        {
            //posted value is obtained from the model
            DatePickerValue.value = model.value;
            return View(DatePickerValue);
        }
    }
}

Selected value will be retrieved as below.

DatePickerFor Component in ASP.NET Core