0

I've added jQueryUI DatePicker on HtmlTextBoxFor in my MVC application. Below is my code. Now when I debug, The date shows 01/01/2001 12:00:00 in the create event instead of the date selected.

<!-- JoinDate -->
<div class="form-group">
    <div class="editor-label">
        @Html.LabelFor(model => model.JoinDate, new { @class = "col-sm-2 control-label" })
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.JoinDate, new { @class = "form-control", @id = "datepicker" })
    @Html.ValidationMessageFor(model => model.JoinDate)
</div>

<script src="~/Content/js/jquery.js"></script>
<script src="~/Scripts/jquery-ui-1.11.4.min.js"></script>

<script>
$(function () {
    $("#datepicker").datepicker();
});

6
  • You 've referenced jQuery-ui twice, you need to remove either one of the reference. I suggest remove <script src="~/Scripts/jquery-ui-1.11.4.js"></script> Commented Apr 27, 2016 at 2:09
  • Still getting value 01/01/... Commented Apr 27, 2016 at 2:12
  • What if you declare it like: $(function () { $("#datepicker").datepicker({defaultDate: ''}); }); Commented Apr 27, 2016 at 2:17
  • No it does not help. Commented Apr 27, 2016 at 2:23
  • 1
    What is the value of JoinDate when you pass the model to the view? Commented Apr 27, 2016 at 3:39

1 Answer 1

0

Well I found a solution to this. I added an input and assign it jQueryUI datepicker. Now onSelect event, it just set the value to the TextBoxfor. See the code below.

<script>
$(function () {
    $("#datepicker").datepicker();
});

<script>
$('#datepicker').datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "dd-mm-yy",
    constrainInput: true,
    defaultDate: new Date(1975, 1 - 1, 1),
    yearRange: "2000:2025",
    onSelect: function () {
        alert($('#datepicker').val());
        $('#joindate').val($('#datepicker').val());
    }
});
</script>

<!-- JoinDate -->
                        <div class="form-group">
                            <div class="editor-label">
                                @Html.LabelFor(model => model.JoinDate, new { @class = "col-sm-2 control-label" })
                            </div>
                            <div class="editor-field">
                                <input type="text" id="datepicker" onchange="onDateChange()"/>
                                @Html.TextBoxFor(model => model.JoinDate, new { @class = "form-control", @id="joindate" })
                                @Html.ValidationMessageFor(model => model.JoinDate)
                            </div>
                        </div>
Sign up to request clarification or add additional context in comments.

4 Comments

There is no reason to use defaultDate: - If the value of property JoinDate is set in the controller before you pass the model to the view, then that value will be displayed.
Actually this controller is to create. When I use it in View, I would not need datepicker.
That makes no sense. Why are you asking a question about something that's not needed? And why are you using new { id="joindate" } - the id is already id="JoinDate"
Thanks for the second advice. I actually did not know that. For the first thing you said, I know people here don't ask anything that is not needed. I need to implement datepicker for Create controller. In View details, I just show the date in Textboxfor. No need to change it in View.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.