Include DropDown List Inside WebGrid in Web API

Introduction

This article describes including the Dropdown list in the WebGrid in the Web API. The WebGrid is used for displaying the data in the table form. It provides the custom formatting for the tabular data. Here we use the WebGrid in which we include the Dropdown list.

The following is the procedure for creating the application.

Step 1

First create a Web API application using the following:

  • Start Visual Studio 2013.
  • From the Start window select "New Project".
  • From the new Project window select "Installed" -> "Visual C#" -> "Web" -> "Visual Studio 2012".
  • Select "ASP.NET MVC4 Web Application" and click the "OK" button.

    l.jpg
  • From the "MVC4 Project" window select "Web API".
    l1.jpg
  • Click the "Ok" button.

Step 2

Now add a Model class.

  • In the "Solution Explorer".
  • Right-click on the "Model Folder".
  • Select "Add" -> "Class".
  • From the add item window select "Installed" -> "Visual C#".

    l2.jpg
  • Select "Class" and click on the "Add" button.

Add 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 WebGridList.Models  
  7. {  
  8.    public class EmployeeInformation  
  9.     {  
  10.         public List<Employee> EmployeeModel { getset; }  
  11.     }  
  12.     public class Employee  
  13.     {  
  14.         public string Name { getset; }  
  15.         public string Address { getset; }  
  16.         public SelectList MerritalStatus { getset; }  
  17.     }  
  18.     public class Status  
  19.     {  
  20.         public int ID { getset; }  
  21.         public string StatusName { getset; }  
  22.     }  
  23. } 

In the code above here we added a line of code  "public SelectList MerritalStatus { get; set; }" in which "selectList" describes the list of the items in which the user selects an item.

Step 3

Now in the "Controller" we add some code as in the following:

  • In the "Solution Explorer".

  • Expand the "Controller" folder.

  • Select the "HomeController".

    l3.jpg

Add 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. using WebGridList.Models;  
  7. namespace WebGridList.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ActionResult Index()  
  12.         {  
  13.             EmployeeInformation empobj = new EmployeeInformation();  
  14.             empobj.EmployeeModel = MerryDetail();  
  15.             return View(empobj);  
  16.         }  
  17.         public List<Employee> MerryDetail()  
  18.         {  
  19.             List<Employee> objempmodel = new List<Employee>();  
  20.             objempmodel.Add(new Employee { Name = "Employee1", Address = "Address1", MerritalStatus = GetStatus() });  
  21.             objempmodel.Add(new Employee { Name = "Employee2", Address = "Address2", MerritalStatus = GetStatus() });  
  22.             objempmodel.Add(new Employee { Name = "Employee3", Address = "Address3", MerritalStatus = GetStatus() });  
  23.             objempmodel.Add(new Employee { Name = "Employee4", Address = "Address4", MerritalStatus = GetStatus() });  
  24.             objempmodel.Add(new Employee { Name = "Employee5", Address = "Address5", MerritalStatus = GetStatus() });  
  25.             return objempmodel;  
  26.         }  
  27.         public SelectList GetStatus()  
  28.         {  
  29.             List<Status> status = new List<Status>();  
  30.             status.Add(new Status { ID = 1, StatusName = "Merried" });  
  31.             status.Add(new Status { ID = 2, StatusName = "Unmerried" });  
  32.             SelectList objinfo = new SelectList(status, "ID""StatusName");  
  33.             return objinfo;  
  34.         }  
  35.     }  
  36. } 

Step 4

In the View Write some code as in the following:

  • In the "Solution Explorer".

  • Expand the "Views" folder.

  • Select "Home" -> "Index.cshtml".

l4.jpg

Add the following code:

  1. @model WebGridList.Models.EmployeeInformation  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4. }  
  5. <style type="text/css">  
  6.     .gridTable {  
  7.         margin: 4px;  
  8.         padding: 10px;  
  9.         border: 1px #333 solid;  
  10.         border-collapse: collapse;  
  11.         min-width: 550px;  
  12.         background-color: #999;  
  13.         color: #999;  
  14.     }  
  15.     .gridHead th {  
  16.         font-weight: bold;  
  17.         background-color: #ffd800;  
  18.         color: #333;  
  19.         padding: 10px;  
  20.     }  
  21.     .gridHead a:link, .gridHead a:visited, .gridHead a:active, .gridHead a:hover {  
  22.         color: #333;  
  23.     }   
  24.     .gridHead a:hover {  
  25.         text-decoration: underline;  
  26.         background-color:#f67f7f  
  27.     }  
  28.     .gridTable tr.gridAltRow {  
  29.        background-color: #c7d1d6;  
  30.     }  
  31.     .gridTable tr:hover {  
  32.         background-color: #f67f7f;  
  33.     }  
  34.     .gridAltRow td {  
  35.         padding: 10px;  
  36.         margin: 5px;  
  37.         color: #333;  
  38.     }  
  39.     .gridRow td {  
  40.         padding: 10px;  
  41.         color: #333;  
  42.     }  
  43.     .gridFooter td {  
  44.         padding: 10px;  
  45.         background-color: #c7d1d6;  
  46.         color: #999;  
  47.         font-size: 12pt;  
  48.         text-align: center;  
  49.     }  
  50.     .gridFooter a {  
  51.         font-size:medium;  
  52.         font-weight: bold;  
  53.         color: #333;  
  54.         border: 1px #333 solid;  
  55.     }  
  56. </style>  
  57. @{  
  58.   var g_style = new WebGrid(source: Model.EmployeeModel,   
  59.     rowsPerPage: 10);  
  60.     @g_style.GetHtml(  
  61.         tableStyle: "gridTable",  
  62.         headerStyle: "gridHead",  
  63.         footerStyle: "gridFooter",  
  64.         rowStyle: "gridRow",  
  65.         alternatingRowStyle: "gridAltRow",  
  66.         columns: g_style.Columns(  
  67.         g_style.Column("Name""Name"),  
  68.         g_style.Column("Address""Address"),  
  69.         g_style.Column(header: "Status", format: @item => Html.DropDownList("value", (IEnumerable<SelectListItem>)Model.EmployeeModel[0].MerritalStatus))))  
  70.  

Step 5

Execute the application. The output will be:

l5.jpg

Select the dropdown list.

l6.jpg


Similar Articles