Introduction
This article describes how to add a calendar in the Web API. Here we use the jQuery Calendar Control in the Web API. In it I show how to transfer the date of the calendar to the controller. We use the Calendar Control for showing the calendar in our application Through this calendar the user can point to any day in any year.
Now we will see the procedure for adding the calendar in the Web API.
Step 1
Create a Web API application:
- Start Visual Studio 2012.
- From the Start Window select "Installed" -> "Visual C#" -> "Web".
- Select "ASP.NET MVC 4 Web Application" and click on the "OK" button.
- From the "MVC4 Project" Window select "Web API".
- Click on the "OK" button.
Step 2
Create a Model Class "Calendar.cs":
- In the "Solution Explorer".
- Right-click on the "Models" -> "Add" -> "Class".
- Select "Installed" -> "Visual C#" and select class.
- Click on the "Add" button.
Add the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CalendarWebAPI.Models
{
public class Calendar
{
[Required(ErrorMessage = "Date Must be Require")]
public string Date
{
get;
set;
}
}
}
In the code above "Required" shows that the Date field is required.
Step 3
In the "HomeController" write some code. This file exists in:
Add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CalendarWebAPI.Models;
namespace CalendarWebAPI.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public string DisplayDate(Calendar calendar)
{
if (ModelState.IsValid)
{
return "Date have been selected by you : <b>" + calendar.Date.ToString() + "</b>";
}
return "Please select any date ";
}
}
}
In the code above , there is an action method "Index" that returns a view and the "DisplayDate()" method returns the date selected by you, otherwise it returns a message "Please select any date".
Step 4
Now we add an "MVC 4 View Page(ASPX)" named "Index.aspx".
Add the following code:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CalendarWebAPI.Models.Calendar>" %>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Index</title>
<script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.ui.datepicker.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#datepicker").datepicker();
});
</script>
</head>
<body>
<% using (Html.BeginForm("DisplayDate", "Home", FormMethod.Post))
{%>
<div>
<div class="demo">
<p>
Date:
<%: Html.TextBoxFor(d => d.Date, new { id = "datepicker" })%>
<input type="submit" value="Display" />
</p>
</div>
<%} %>
</div>
</body>
</html>
In the code above we use the three jQuery files for adding the Calendar Control that are "jquery-1.8.2.min.js" , "jquery-ui-
1.8.16.custom.min.js" and "jquery.ui.datepicker.js". Here we also create a JavaScript function that calls the "datepicker" function.
When we click on the Display button then it calls the "DisplayDate" view and displays the selected date.
Step 5
Now execute the application for seeing the output by pressing "F5". The output will be as:
When we move the cursor on the TextBox and click it, it shows the calendar.
Select a date and click on the "Display" button.
Click on "Previous"; it then displays the previous month.
If we click on "Next" then it displays the next month.