Adding the CheckBox Control in Web API


This article shows how to use the Checkbox Control in the ASP.NET Web API. The checkbox control is used for selecting one or more items. Here I provide an example of adding the checkbox control.

Checkbox Control

We can add the two types of properties with this control, either integer or string. We can say that it is a two-state Checkbox. We create a control table that has a property column in which we insert the property name. When we select any state from the checkbox then it sets the property either as a value in the value column of this Checkbox table or specified as an initial value of the property in the property table. If there is no initial value of the selected property then it uses 1 and the unselected states are set to null.

Now for the procedure for adding the Checkbox control in the Web API.

Step 1

Create a Web API application:

  • Start Visual Studio 2012.
  • From the start Window select "New Project".
  • In the Template Window select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web application" and click the "OK" button.


  • From the MVC4 Project window select "Web API".


  • Click the "Ok" button.

Step 2

In the "HomeController" we specified the list of the items. This controller exists in:

  • In the "Solution Explorer".
  • Select "Controller" -> "HomeController".


Write the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace CheckList.Controllers  
  7. {  
  8.     public class HomeController : Controller  
  9.     {  
  10.         public ActionResult Index()  
  11.         {  
  12.             Dictionary<intstring> StateList = new Dictionary<intstring>();  
  13.             StateList.Add(1, "Utter Pradesh");  
  14.             StateList.Add(2, "Delhi");  
  15.             StateList.Add(3, "Assam");  
  16.             StateList.Add(4, "Rajasthan");  
  17.             StateList.Add(5, "Andra Pradesh");  
  18.             StateList.Add(6, "Kerala");  
  19.             StateList.Add(7, "Madhya Pradesh");  
  20.             StateList.Add(8, "chhattisgarh");  
  21.             StateList.Add(9, "Orissa");  
  22.             ViewBag.DataSource = StateList;  
  23.             return View();  
  24.         }  
  25.     }  
  26. } 

Step 3

In the view "index.cshtml", write the function for selecting the items and add the table for the checkbox items. This view exists in the:

  • In the "Solution Explorer".
  • Select "View" -> "Home" -> "index.cshtml".


Use the following code:

  1. <h2>CheckLIst in Web API</h2>  
  2. <script>  
  3.     //Get value of selected items  
  4.     function Selectstate() {  
  5.         var Itemselected = [];  
  6.         $("[id*=CheckBoxList1] input:checked").each(function () {  
  7.             Itemselected.push($(this).val());  
  8.         });  
  9.         if (Itemselected.length > 0) {  
  10.             alert("Selected States: " + Itemselected);  
  11.         } else {  
  12.             alert("No One state selected:");  
  13.         }  
  14.     }  
  15. </script>  
  16.  <table id="CheckBoxList1">  
  17.     @foreach (var item in ViewBag.DataSource)  
  18.     {  
  19.         string controlId = "CheckBoxList1_" + @item.Key;  
  20.         <tr>  
  21.             <td>  
  22.                 <input id="@controlId" type="checkbox"  value="@item.Key" />@item.Value  
  23.             </td>  
  24.         </tr>  
  25.     }  
  26.     </table>  
  27. <input type="button" value="submit" id="demo" onclick="javascript: Selectstate();" />   

In the code above the function "Selectstate" is for selecting the items from the checkbox list and storing them in the variable "Itemselected". Now move to the "if" statement in which the length of the variable "Itemselected" is checked; if the length is greater than 0 (Zero) then display the selected states otherwise display No one state selected.

Step 4

Now execute the application. Press "F5". The output window looks like this:


Now select the items and click on the "Submit" button, it displays a message with the id of the selected item.


If no items are selected then it displays another message.