In this article I will explain with an example, how to implement Bootstrap DatePicker (Calendar) in ASP.Net Core MVC.
This article will also illustrate how to get the selected Date of the Bootstrap DatePicker (Calendar) inside Controller on Button click in ASP.Net Core MVC.
The Date format for the selected Date will be set to dd/MM/yyyy format.
 
 
Bootstrap DatePicker Plugin
This article makes uses of the DatePicker for Bootstrap plugin.
 
 
Controller
The Controller consists of two 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 the Form is posted. The value of the selected date from the Bootstrap DatePicker is fetched and it set into a ViewBag object.
The value of the ViewBag object will be used for displaying the selected date in View using JavaScript Alert Message Box.
public class HomeController : Controller
{
    // GET: Home
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Index(string selectedDate)
    {
        ViewBag.Message = "Selected Date: " + selectedDate;
        return View();
    }
}
 
 

View
The View consists of an HTML Form which has been created using the Razor Tag attributes 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.
Inside the Form, there is a TextBox and a Submit Button. The TextBox has been made ReadOnly and has been applied with the Bootstrap DatePicker Plugin.
When the date is selected and the Submit button is pressed, the Form is submitted and the value of the selected date is returned in a ViewBag object which 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>
</head>
<body>
    <!-- Bootstrap -->
    <script type="text/javascript" src='https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js'></script>
    <script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js'></script>
    <link rel="stylesheet" href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css'
          media="screen"/>
    <!-- Bootstrap -->
    <!-- Bootstrap DatePicker -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css" type="text/css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js" type="text/javascript"></script>
    <!-- Bootstrap DatePicker -->
 
    <form asp-controller="Home" asp-action="Index" method="post">
        <input type="text" id="txtSelectedDate" name="SelectedDate" readonly="readonly"/>
        <input type="submit" value="Submit"/>
 
        <script type="text/javascript">
            $(function () {
                $('#txtSelectedDate').datepicker({
                    changeMonth: true,
                    changeYear: true,
                    format: "dd/mm/yyyy",
                    language: "tr"
                });
            });
        </script>
        @if (ViewBag.Message != null)
        {
            <script type="text/javascript">
            $(function () {
                alert("@ViewBag.Message");
            });
            </script>
        }
    </form>
</body>
</html>
 
 
Screenshot
ASP.Net Core: Bootstrap DatePicker (Calendar) implementation
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Downloads