jQuery DatePicker inline mode in MVC3 Razor

  1. In this article we will see how to render jQuery DatePicker in inline mode.
  2. Generally datepicker opens up on click of button or on clicking inside textbox.
  3. The inline display the datepicker embedded in the page instead of in an overlay. 
  4. We need to call  .datepicker() on a div instead of an input to achieve inline datepicker.
  5. We will see how we can render Inline DatePicker in MVC3 Razor application.

Following are the steps :

ViewModel :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace jQueryDatePicker.Models
    public class Register
        public DateTime DOB { get; set; }

The above is our ViewModel. The ViewModel contains a property which is of type DateTime. As you can see we have define DataAnnotation property UIHint which indicates to call EditorTemplate with name DOB to render control for this property. In EditorTemplate we are rendering a div element.

View :

@model jQueryDatePicker.Models.Register
    Layout = "../Shared/_Layout.cshtml";

<!DOCTYPE html>

@using (Html.BeginForm("DatePicker", "DatePicker"))
@Html.EditorFor(Model => Model.DOB)
<input type="submit" value="Submit" />

<script type="text/javascript">

    $(document).ready(function () {


EditorTemplate :

<div id="DOB"></div>

Layout :

<!DOCTYPE html>
    <meta charset="utf-8" />
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.datepicker.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.widget.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />


Include the above jquery files in your layout referred by View.

Demo :

Using the Div instead of textbox, we could render datepicker in inline mode i.e. embedded on page. 


Post a Comment