JQuery UI Datepicker in MVC 4
Today,
I spent couple of hours in finding solution to enable the use of Datepicker in MVC 4 Application.
If you create a MVC 4 application using ‘Internet’ template, you will find ‘BundleConfig.cs’ file in ‘App_Start’ folder, open it.
If you create a MVC 4 application using ‘Internet’ template, you will find ‘BundleConfig.cs’ file in ‘App_Start’ folder, open it.
You
will notice there is total 6 different bundles (including jquery and css) being
processed. Now, open ‘_Layout.cshtml’ file:
You
will notice only 3 bundles are being used on views out of 6, that's good but it will not enable you using datepicker in your app. You need 2 more bundles references on the view page where you wish datepicker to appear.
So, open your view page and type following code in @section Scripts {}.
Please note 'Dob' is the id attribute of textbox.
Now if you run the application you will see your Datepicker working.
So, open your view page and type following code in @section Scripts {}.
@section
Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/themes/base/css")
<script type="text/javascript">
$(document).ready(function () {
$("#Dob").datepicker({
changeMonth: true,
changeYear: true
});
});
</script>
}
Please note 'Dob' is the id attribute of textbox.
Now if you run the application you will see your Datepicker working.
Great post it is successfully fix my issue about this component . Thanks
ReplyDeleteGreat. Been looking for a solution for days ...
ReplyDeleteThanks.
Markus
Great Answer....Excellent ...Now my Datepicker was working
ReplyDeleteWorking!! very thanks, excellent post!
ReplyDeletei cant open the datepicker...
ReplyDeleteThanks for the post. It is working for me.
ReplyDeleteThanks!
ReplyDeleteThank you so much for the post!..
ReplyDeleteThanks,
ReplyDeleteWhat must we do to put a datepicker to 2 textbox?
Add another .datepicker method inside above $(document).ready function.
DeleteOr use class selector, and apply the same class to both textboxes. This way you can can attache that datepicker to both with single instance of code.
DeleteExample:
$(document).ready(function () {
$(".DateField").datepicker({
changeMonth: true,
changeYear: true
});
});
The years only seem to span 10 years in either direction, is there a way to change that to suit more a DOB if people are like 30 or 40?
ReplyDeleteyou can use the yearRange attribute to fix that
Deleteexample:
$(document).ready(function () {
$("#Dob").datepicker({
changeMonth: true,
changeYear: true,
yearRange: "1950:2013"
});
});
i have a problem when i give year range in datepicker year dropdown select year range but when i select date by default they pick cuurent date or month...plz give solution..
Deletethanks for your good post
ReplyDeleteTypeError: $(...).live is not a function
ReplyDeleteThank you for the post - very helpful!
ReplyDeleteI tried the same for MVC4 Mobile template,But its not working
ReplyDeleteThanks ! Works great !!!
ReplyDeleteYou saved my day.
Hi @Abhimanyu
ReplyDeleteThis is a good article. However, i am getting some problems when i tried to implement this.
1. First of all the jquery datepicker is showing up but its without a background (like it appears as a transparent dropdown).
2. Due to this transparent behaviour, i am unable to see it properly on the screen.
3. i am also getting errors like this in firefox when i run this example.
Error: SyntaxError: syntax error
Source File: http://localhost:52350/Content/themes/base/jquery.ui.datepicker.css
Line: 10
Source Code:
.ui-datepicker { width: 17em; padding: .2em .2em 0; display:
4. Please also let me know which attribute we should use to change the date which is being displayed in the textbox.
Simple but very useful indeed. Thanks
ReplyDeleteShamim
Thank you!
ReplyDeleteFirst time that it works.
Annette
Nice Post Man!
ReplyDeleteVery nice article, works fine. Thank you very much. Keep up the good work.
ReplyDeleteworks great
ReplyDeletethanks a lot
this tips helped me
It does not work well with jquery mobile, it changes the css. button icons no longer show up.
ReplyDeleteYou are genius, you saved my day, thanks a lot :-)
ReplyDeleteI tried the same for MVC4 Mobile template,But its not working
ReplyDeleteBut it is not working for Editorfor. How can I use it for EditorFor?
ReplyDeleteHow can I use it for EditorFor Control ?
ReplyDeleteAbhimanyu Kumar Vatsa thank you soo much it realy help me.
ReplyDeletethanks Abhimanyu Kumar Vatsa it really helps me
ReplyDeleteawesome!
ReplyDeletesimply wonderful man!!!thank you thank you..thanks a lot..been searching for days for this solution
ReplyDeleteThanks you thank you!!! Please how to display the date in this format: Day/Month /Year ex:15/04/2014
ReplyDeletedear abhimanyu i am tired of date picker
ReplyDeletethe datepicker doesnt appear instead a default template appears
01/01/0001
i tried the same code and it worked in windows 7 on friends laptop mine is win 8 i dont know whether it is problem of os or visual studio
please help me i am tired of mvc4 datepicker not working instead this apppears in textbox 01/01/001
ReplyDeletei use vs2010 ultimate and windows 8
is it the probliem of os or vs
thanks
ReplyDeleteGreat
ReplyDeleteThank u, what a great tutorial, I have been looking this simple explanation all my day
ReplyDeleteHow can i use for EditiorForControl???????????????????
ReplyDeleteThankfull broooooo
ReplyDeletethanks for your nice tutorial....helped me a lot
ReplyDeletehi thanks for the tutorial. how do i get those files into my solution folders?
ReplyDelete