In this article I will explain with an example, how to implement
jQuery DateTimePicker in ASP.Net Core MVC.
Download jQuery DateTimePicker Plugin
You will need to download the plugin files from the following location.
The complete documentation is available on the following page.
Plugin files Location
The Plugin files are stored inside the plugin Folder (Directory) of wwwroot Folder (Directory).
Allowing access to Static Files in ASP.Net Core
Inside the Program.cs class, you will need to allow Static files by calling the function UseStaticFiles. Unless this function is called, the Static files will not be accessible.
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
This Action method gets called, when Submit Button is clicked.
Inside this Action Method, the selected Date and Time is fetched from the TextBox and set into a
ViewBag object.
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(string selectedDateTime)
{
DateTime dt = Convert.ToDateTime(selectedDateTime);
ViewBag.Message = "Selected Date and Time: " + dt.ToString();
return View();
}
View
Inside the View, first the ASP.Net TagHelpers is inherited.
The View consists of an HTML Form which has been created using the ASP.Net TagHelpers with the following attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The following HTML Form consists of a TextBox and a Submit Button.
jQuery DateTimePicker Plugin implementation
Inside the View, the following
jQuery DateTimePicker CSS script file is inherited.
1. jquery.datetimepicker.css
And then, the following
jQuery and
jQuery DateTimePicker JS files are inherited.
1. jquery.min.js
2. jquery.datetimepicker.full.js
Inside the
jQuery document ready event handler, the TextBox has been applied with the
jQuery DateTimePicker plugin.
Submitting the Form
When the Date and Time is selected and the
Submit button is clicked, the Form is submitted and the
ViewBag object is checked for NULL and if it is not NULL then, the value of the
ViewBag object is displayed using JavaScript Alert Message Box.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/plugin/jquery.datetimepicker.css" rel="stylesheet" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="~/plugin/jquery.datetimepicker.full.js"></script>
<script type="text/javascript">
$(function () {
$("#txtDateTime").datetimepicker();
});
</script>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
<input type="text" name="selectedDateTime" id="txtDateTime" />
<input type="submit" value="Submit" id="btnSubmit" />
</form>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.Message");
};
</script>
}
</body>
</html>
Screenshots
The Form
DateTime object displaying the selected DateTime
Demo
Downloads