We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

assign values to datepicker in Ajax success function

Hi Team,

Could you please advise how to assign min, value and max property values to datepicker in Ajax success function?
I tried below code but the appendTo code creates new datepicker controller each time the Ajax success function fires, see screenshot.

var currentYear = today.getFullYear(); var currentMonth = today.getMonth(); var currentDay = today.getDate(); var datepicker = new ej.calendars.DatePicker({ min: new Date(currentYear, currentMonth, 7), max: new Date(currentYear, currentMonth, 27), value: new Date(currentYear, currentMonth, 14) }); datepicker.appendTo('#datepicker');


Thanks

1 Reply

CI Christopher Issac Sunder K Syncfusion Team February 12, 2019 11:57 AM UTC

Hi Customer, 

Thank you for contacting Syncfusion support. 

We have prepared the sample for assign the min and max values to the DatePicker component on AJAX success method. Please check with the below code example. 

[index.cshtml] 
<div> 
    <input type="text" id="datepicker" />   
    <div> 
        <input type="button" id="btnGet" value="Submit" /> 
    </div> 
</div> 
 
<script> 
    var today = new Date(); 
    var currentYear = today.getFullYear(); 
    var currentMonth = today.getMonth(); 
    var currentDay = today.getDate(); 
 
    //DatePicker creation 
    var datepicker = new ej.calendars.DatePicker({ 
        min: new Date(currentYear, currentMonth, 7), 
        max: new Date(currentYear, currentMonth, 27), 
        value: new Date(currentYear, currentMonth, 14), 
        width: "250px" 
    }); 
    datepicker.appendTo('#datepicker'); 
 
    // Ajax method to set the min and max value 
    $(function () { 
        $("#btnGet").click(function () { 
            $.ajax({ 
                type: "POST", 
                url: "/Home/AjaxMethod", 
                data: '{}', 
                contentType: "application/json; charset=utf-8", 
                dataType: "json", 
                success: function (response) { 
                    // Create the instance for the DatePicker 
                    var dateObj = document.getElementById('datepicker').ej2_instances[0]; 
                    // Assign the min and max value to DatePicker 
                    dateObj.min = new Date(parseInt(response.min.substr(6))); 
                    dateObj.max = new Date(parseInt(response.max.substr(6))); 
                } 
            }); 
        }); 
    }); 
</script> 
 

        
[HomeController.cs] 
 
       public class Datemodel 
        { 
            ///<summary> 
            /// Gets or sets min. 
            ///</summary> 
            public DateTime? min { get; set; } 
 
            ///<summary> 
            /// Gets or sets max. 
            ///</summary> 
            public DateTime? max { get; set; } 
        } 
 
        [HttpPost] 
        public JsonResult AjaxMethod(DateTime? min, DateTime? max) 
        { 
            // pass the min and max values to the component 
            Datemodel model = new Datemodel 
            { 
                min = DateTime.Parse("2/1/2019"), 
                max = DateTime.Parse("3/10/2019") 
            }; 
            return Json(model); 
        } 

Please check the sample from the below link. 

 
Please let us know if you require any further assistance. 

Thanks, 
Christo 


Loader.
Live Chat Icon For mobile
Up arrow icon