2

Issue: I have a search page that is looking data that occurs in a range. There is a "From" and a "To" datepicker input in the view. I want to default the selection of these values to the corresponding "From" and "To" dates passed in with the view model. The correct value from the view model is loading into the view's input control, but the date picker does not display the value, it only shows an input mask of mm/dd/yyyy. If I take out or change the " type=" value to "text" instead of date, the correct date values display, but then the datepicker does not.

Here is my editor template:

@model DateTime?

@Html.TextBox("", Model, "{0:MM/dd/yyyy}", new { @class = "datepicker form-control", type = "date" })

Here is the class property:

    [DataType(DataType.Date)]
    public DateTime? DateTakenFrom { get; set; }

Here is the view:

@Html.EditorFor(model => model.Criteria.DateTakenFrom)

Here is what renders:

<input class="datepicker form-control" data-val="true" data-val-date="The field DateTakenFrom must be a date." id="Criteria_DateTakenFrom" name="Criteria.DateTakenFrom" type="date" value="03/17/2014">

1 Answer 1

1

The date is a proposed type in HTML5 (source: Mozilla Developer Network), but it is not implemented well in most browsers, so I discourage its use. Use some Javascript plugin with type="text". Take note, that there are existing plugins for Bootsrap version 2 and 3, and they are not compatible.

Sign up to request clarification or add additional context in comments.

5 Comments

If I remove the type, then the datepicker stops working. I'm currently using Bootstrap.Datepicker v1.3.0 as installed from the Nuget package manager. If I pull that datepicker out, do you have a recommendation on one to use?
Do you use bootstrap 2?
We were starting a new project, so we went with the latest at the time.. Bootstrap v3.1.1
Then you might want to use jQuery UI for this task. Here is an example: jsfiddle.net/jasenhk/4g9u5. Or you can use a more Bootstrap-like solution, like: bootply.com/87089.
Uninstalled Bootstrap.Datepicker v 1.3.0 via the package manager and then manually installed the datePicker from here eyecon.ro/bootstrap-datepicker. Things appear to be in order now. Per your initial recommendation I did remove the type=date from the editor template as well....Thanks

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.