Header Ads

ASP.NET MVC5: Time Picker Plugin

In most of the scenarios, time is usually auto-generated component of the product, but, occasionally, there comes a situation where time input is required from the end-user. For such scenarios, there is a very simple and interactive JQuery plugin Bootstrap Time Picker available. This plugins adds interactivity to the end-user choice of providing time input.

Today, I shall be demonstrating integration and basic usage of the Bootstrap Time Picker JQuery plugin in ASP.NET MVC5 platform.


Prerequisites:

Following are some prerequisites before you proceed any further in this tutorial:
  1. Download Bootstrap Time Picker JQuery Plugin.
  2. Knowledge of ASP.NET MVC5.
  3. Knowledge of HTML.
  4. Knowledge of JavaScript.
  5. Knowledge of Bootstrap.
  6. Knowledge of Jquery.
  7. Knowledge of C# Programming.
The running working source code solution for this article is being developed in Microsoft Visual Studio 2017 Professional. For the source code click on the download button below.

Download Now!

Let's begin now.

1) Create a new MVC web project and name it "MvcTimePicker".  

2) Make changes to the "Views->Shared->_Layout.cshtml" file according to your business requirements.

3) Now, create a new "Models\TimerViewModel.cs" file and add relevant properties for this model i.e.

...

    /// <summary>
    /// Timer view model class.
    /// </summary>
    public class TimerViewModel
    {
        ... 
        
        /// <summary>
        /// Gets or sets the HTML content property.
        /// </summary>
        [Required]
        [Display(Name = "Time")]
        public string Timer { get; set; }

        ...
    }

...

In the above code, I have created my view model which I will attach to my view. Here, I have created string type Timer property which will capture time input from the end-user.

4) Create a new "Controllers\TimerController.cs" file and code logic to format time.

5) Now, create a view "Views\Timer\Index.cshtml" file and add your code for time picker page in order to get the input from the end-user i.e.

...

         <div class="input-group">
              <span class="input-group-addon">Time</span>
              <input id="timerId" type='text' class="form-control text-center" placeholder="Time" readonly="readonly" />
              <span class="input-group-addon">
                  <span class="fa fa-clock-o"></span>
              </span>
          </div>

...

In the above code, I have created a simple view for taking time input from the end-user via Bootstrap Time Picker Jquery plugin.

6) Create "Scripts\script-custom-timepicker.js" file and add the code to integrate the jquery time picker plugin i.e.

$(document).ready(function ()
{
    ...

    var timerVal = moment().format('hh:mm A');

    ...

    $('#timerId').timepicker(
    {
            template: 'dropdown',
            minuteStep: 1,
            secondStep: 1,
            showMeridian: true,
            defaultTime: timerVal
   });

   ...  
   
});

In the above code, I have created basic Bootstrap Time Picker JQuery plugin settings to integrate the plugin with asp.net mvc platform.

7) Now, execute the project and you will be able to see the following in action i.e.


Conclusion

In this article, you will learn to integrate Bootstrap Time Picker JQuery plugin with ASP.NET MVC5 platform. You will also learn about the basic usage of the time picker plugin and also learn to take time input from end-user and apply proper date/time format to the provided time input.

No comments