Pass (Send) jQuery DatePicker selected value to Controller in ASP.Net MVC

Last Reply on Apr 11, 2019 03:16 AM By dharmendr

Posted on Apr 10, 2019 04:24 PM

Hey everyone,

I have a datepicker and when I don't use datepicker I get the date value in my view but with it it just displays the format MM/dd/yyyy so I am wondering how can I get my datepicker to show the saved value when I load the view.

        [Required]
        [Display(Name = "Username")]
        public string UserName { get; set; }

        [DataType(DataType.Date),
            DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
            ApplyFormatInEditMode = true)]
        [Display(Name = "Birthday")]
        public DateTime Birthday { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Date Created")]
        public DateTime DateCreated { get; set; } 

 

    <script>
        $(function () {
            // DATEPICKER JQUERY ADDITION
            $(".datepicker").datepicker({
                dateFormat: "MM/dd/yy",
                changeYear: true,
                shownOn: "button"
            }).css("display", "inline-block")
                .next("button").button({
                    icons: { primary: "ui-icon-calendar" },
                    label: "Select a Date",
                    text: false
                });
            // DATETIMEPICKER JQUERY ADDITION
            $(".datetimepicker").datetimepicker({
                dateFormat: 'MM/dd/yyyy',
                timeFormat: 'hh:mm:ss',
                timeInput: true,
            });
        })
    </script> 
    <div class="col-md-10">
        @Html.EditorFor(model => model.Birthday, new { htmlAttributes = new { @class = "form-control datepicker" } })
        @Html.ValidationMessageFor(model => model.Birthday, "", new { @class = "text-danger" })
    </div>

 

This question does not have replies that have been liked.