I'm already using this datepicker on another view, a Create() view that receives a model then I can show the datepicker by using:
<link rel="stylesheet"href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
...
@Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control", placeholder = "OrderDate", @readonly = "true" } })
...
@section Scripts
{
@Scripts.Render("~/bundles/jqueryui")
<script type="text/javascript">
jQuery.validator.methods["date"] = function (value, element) { return true; }
$(document).ready(function () {
$("#OrderDate").val("");
$('input[type=datetime]').datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: "-1:+2",
onClose: function (dateText, inst) {
$("#cmdEnter").focus();
}
});
});
</script>
}
Now the problem is that i need to use the datepicker in a View as one of the search fields on top, i'm trying to implement a date search by range. This view is an Index, meaning it doesn't receive a model it receives a model collection (PageList in this case):
@model PagedList.IPagedList<EntregaMedicamentos.Models.PedidoDisprofarmaViewModel>
So, in this case I can't use model => model.OrderDate, then I tryied using the Viewbag to pass that date there, something like..
@Html.TextBox("searchDateFrom", ViewBag.currentFilter2 as DateTime?, new { @class = "form-control", placeholder = "Desde fecha", @readonly = "true" })
So I changed from EditorFor to TextBox and also tried with Editor but the datepicker still doesn't want to pop-up on click, any ideas?
This is what a tried, still no pop-up:
@Html.TextBox("searchDateFrom", ViewBag.currentFilter2 as DateTime?, new { @class = "form-control", placeholder = "Desde fecha", @readonly = "true" })
....
@section scripts
{
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/cssjqryUi")
<script type="text/javascript">
jQuery.validator.methods["date"] = function (value, element) { return true; }
$(document).ready(function () {
$("#searchDateFrom").val("");
$('input[type=datetime]').datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: "-1:+2",
onClose: function (dateText, inst) {
$("#cmdSearch").focus();
}
});
});
</script>
...
Thanks!!