I'm trying to make a datepicker which will display years and month only.
I saw this questions in different places but all the solutions I have tested doesn't work.
I follow this example : /
Here is my .cshtml file :
@using Site.Models
@{
ViewBag.Title = "Rapports";
}
<script type="text/javascript">
$(function() {
$('.date-picker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
}
});
});
</script>
<style type="text/css">
.ui-datepicker-calendar {
display: none;
}
</style>
<h2>Rapports</h2>
<div>
@using (Html.BeginForm()) {
<input name="startDate" id="startDate" class="date-picker" />
<input type="submit" value="Rechercher" />
}
</div>
But in my page there is only a textbox and nothing pop up when I click it. I don't know what I am doing wrong.
I'm trying to make a datepicker which will display years and month only.
I saw this questions in different places but all the solutions I have tested doesn't work.
I follow this example : http://jsfiddle/bopperben/DBpJe/
Here is my .cshtml file :
@using Site.Models
@{
ViewBag.Title = "Rapports";
}
<script type="text/javascript">
$(function() {
$('.date-picker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
}
});
});
</script>
<style type="text/css">
.ui-datepicker-calendar {
display: none;
}
</style>
<h2>Rapports</h2>
<div>
@using (Html.BeginForm()) {
<input name="startDate" id="startDate" class="date-picker" />
<input type="submit" value="Rechercher" />
}
</div>
But in my page there is only a textbox and nothing pop up when I click it. I don't know what I am doing wrong.
Share Improve this question edited Feb 13, 2014 at 15:00 Drew Gaynor 8,4825 gold badges41 silver badges53 bronze badges asked Apr 17, 2013 at 9:37 Azzedine HassainiAzzedine Hassaini 3314 gold badges5 silver badges16 bronze badges 5- Are you getting any Javascript error? – Saanch Commented Apr 17, 2013 at 9:54
- No, I don't have any error. – Azzedine Hassaini Commented Apr 17, 2013 at 10:07
- The code you copied was having syntax error. Can you please verify its correct? – Saanch Commented Apr 17, 2013 at 10:11
- 1 Works fine in latest Chrome. Why dont you just create two <select> fields containing years and months? That will work with all browsers for sure. – eosterberg Commented Apr 17, 2013 at 10:12
- have you referenced jQuery and jQueryUI ? – Omu Commented Apr 17, 2013 at 11:04
1 Answer
Reset to default 5Did you start from an ASP.NET MVC4 internet template?
I've tried your code above and got it working on two details.
I added the script tag in a section scripts (that will make sure it renders after jquery is loaded)
@section scripts
{
<script type="text/javascript">
$(document).ready(function() {
$('.date-picker').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
}
});
});
</script>
}
In the _Layout.cshtml I've added the jquerui bundle (script and style) (loads jqueryui in the page)
//In head tag
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/themes/base/css")
//After closing footer tag
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@RenderSection("scripts", required: false)