Add calendar to textbox in MVC in 2 minute

Adding calendar with a text box for new MVC developer is not so easy, I always found in my team people struggle to add a calendar with a text box, while it is as easy as jut to add one line of code and done. It’s true, when first time I try to add a calendar with a text box I also searched many website, read blogs and found, I need to add many jquery files from different locations before using the calendar but that is not true. Microsoft already added everything for us, CSS files, JavaScript and images.

As I am going to show you how you can add calendar by just using some references and just one line of code will complete the task. I created a new MVC application and here are files already available in my application, I have not added any new file manually

alt text

We already have all the images and css files under Content folder see the middle image. And all the JavaScript files under Scripts folder see the third image. Add references for required CSS and JavaScript files to use the calendar

<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" 
         rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" 
         type="text/javascript"></script>

Open index.cshtml file and add these lines, you can add these lines at the page on which you want to add the calendar, i used here id ="anyid" to hook the calendar you can use your own id in textbox and JavaScript function

<p>
    <input type="text" id="anyid" />
</p>

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

Run your application, and click into text box, you will see the calendar start working, here is the output

alt text

Feel free to post comment, suggestion, questions or anything which may help you to use jquery calendar with your code or improve this article for other users.

Note: I used VS 2008 to create this example application, newer version of jquery and css files are available, if you want to use them you can use.

Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • mvc
By Ali Adravi On 03 Feb, 13  Viewed: 6,619

Other blogs you may like

mvc search page example with code

MVC Searh page with pagination: It’s very easy to create a search page in asp.net but when I try to create the same in MVC I faced many problems, how to create model, how to keep searched values in search controls, pagination, I found myself nowhere, so start searching for some good examples but... By Ali Adravi   On 25 Aug 2013  Viewed: 36,927

MVC insert update delete and select records

CRUD (Create, Retrieve, Update and Delete) in MVC. When we start to learn new language, first we try to run an application with “Hello World” and then CRUD functionality. So in this article we will see how to select records from database (with WebGrid, pagination and sort functionality), update a... By Ali Adravi   On 17 Aug 2013  Viewed: 101,191

How to create a single thanks page for entire controller in MVC

Sometimes we need a thanks page say we have user registration, change password, activate account functionality in our application then we need a thanks page after registering with our site, to say thanks for registering with us or showing confirmation that your password is successfully changed or... By Hamden   On 30 Jun 2013  Viewed: 3,282

MVC jquery autocomplete with value and text field

In MVC, autocomplete with jquery is the best way to pull data from database and render according to our requirements, so in this article we will use jquery to show the auto complete text box without any ajax controls. We need a method to get the data from database, a controller method to handle the... By Ali Adravi   On 29 Jun 2013  Viewed: 6,457

Upload files with model data in MVC

Upload multiple files with model data in MVC is really very easy, when I started to test by uploading some files, I though it would be more complicated but it is really not. In my previous post [ASP.Net MVC file upload][1], I promised to post soon about how to upload multiple files. When I was... By Ali Adravi   On 04 Jun 2013  Viewed: 23,734