Cascading Dropdown List In Kendo UI Using ASP.NET MVC And Entity Framework

Introduction

In this article, I’ll describe, how to perform the cascading dropdownlist operation in Kendo UI, using ASP.NET MVC and Entity Framework. The Application is developed, using Entity Framework database first approach and ASP.NET MVC.

Prerequisites - Basic knowledge in ASP.NET MVC, Entity Framework and Kendo UI framework.

Set up the Table - For this article, I have created three tables, whose designs are given below-

Country Table

 
 
 
 
State Table
 
  
 
 
 
City Table
 
 
 
 
Create New MVC Project

Create a new empty ASP.NET MVC Application as per the following figures. Open Visual Studio ->File ->New project ->ASP.NET Web Application.

Select MVC and click OK.

Please refer to my previous article to check out how to configure Kendo UI in ASP.NET MVC Application.

Generate the Model

Now, we will create Entity Framework models from the database tables.

Step 1 - Right-click the Models folder, select Add -> ADO.NET Entity Data Model or select Add->New Item. In the Add New Item Window, select data in the left pane and ADO.NET Entity Data Model from the center pane. Name the new model file. In my case, I made it as Country_State and click Add.

Step 2 - In the Entity Data Model Wizard, select EF Designer from the database and click Next.

  

Step 3 - Click the New Connection button. The Connection Properties Window will open.

 
Step 4 - In the Connection Properties Window, provide the name of the local Server, where the database was created (in this case (DESKTOP-585QGBN)). After providing the Server name, select the Employee from the available databases and click OK.

Step 5 - You can use the default name for the connection to save the Web.Config file and click Next.

 
 
Step 6 - Select table to generate models for Country, State and City table and click Finish.
 


My database schema is shown in the figure, given below-
 
 

Create a Controller

Create a new empty Controller. Right-click the Controllers folder and select Add –> New Empty Controller. In my case, I named it as CascadingCountryController.

Write the code, given below, in the Controller-

  1. public class CascadingCountryController : Controller  
  2.     {  
  3.   
  4.         private  EmployeeEntities db = new EmployeeEntities();  
  5.           
  6.         public ActionResult Index()  
  7.         {  
  8.             return View();  
  9.         }  
  10.         public JsonResult GetCountryList()  
  11.         {  
  12.             return Json(db.tbl_country.Select(x => new { Country_ID = x.Country_ID, Country_Name = x.Country_Name }), JsonRequestBehavior.AllowGet);  
  13.         }  
  14.   
  15.         public JsonResult GetStateList(int? Country)  
  16.         {  
  17.             var State = db.tbl_state.AsQueryable();  
  18.             if (Country>0)  
  19.             {  
  20.                   
  21.                 State = State.Where(s => s.Country_ID == Country);  
  22.                 return Json(State.Select(s => new { State_ID = s.State_ID, State_Name = s.State_Name }), JsonRequestBehavior.AllowGet);  
  23.             }  
  24.             else  
  25.             return Json(db.tbl_state.Select(s => new { State_ID = s.State_ID, State_Name = s.State_Name }), JsonRequestBehavior.AllowGet);  
  26.   
  27.         }  
  28.   
  29.         public JsonResult GetCityList(int? State)  
  30.         {  
  31.             var City = db.tbl_city.AsQueryable();  
  32.             if(State>0)  
  33.             {  
  34.                 City = City.Where(c => c.State_ID == State);  
  35.                 return Json(City.Select(c => new { City_ID = c.City_ID, City_Name = c.City_Name }), JsonRequestBehavior.AllowGet);  
  36.             }  
  37.             else  
  38.             {   
  39.             return Json(db.tbl_city.Select(c => new { City_ID = c.City_ID, City_Name = c.City_Name }), JsonRequestBehavior.AllowGet);  
  40.             }  
  41.         }  
  42.     }  
Creating a View
 
Right click on View-> CascadingCountry folder and add new empty view. Write the code, given below, in view-
  1. @{  
  2.     ViewBag.Title = "Index";  
  3.     
  4. }  
  5.   
  6. <h2>Kendo Cascading Dropdownlist</h2>  
  7.   
  8. <div class="demo-section k-content">  
  9.   
  10.     <h4>Country:</h4>  
  11.     @(Html.Kendo().DropDownList()  
  12.               .Name("Country")  
  13.               .HtmlAttributes(new { style = "width:100%" })  
  14.               .OptionLabel("Select country...")  
  15.               .DataTextField("Country_Name")  
  16.               .DataValueField("Country_ID")  
  17.               .DataSource(source =>  
  18.               {  
  19.                   source.Read(read =>  
  20.                   {  
  21.                       read.Action("GetCountryList", "CascadingCountry");  
  22.                   });  
  23.               })  
  24.     )  
  25.   
  26.     <h4 style="margin-top: 2em;">State:</h4>  
  27.     @(Html.Kendo().DropDownList()  
  28.               .Name("State")  
  29.               .HtmlAttributes(new { style = "width:100%" })  
  30.               .OptionLabel("Select state...")  
  31.               .DataTextField("State_Name")  
  32.               .DataValueField("State_ID")  
  33.               .DataSource(source =>  
  34.               {  
  35.                   source.Read(read =>  
  36.                   {  
  37.                       read.Action("GetStateList", "CascadingCountry")  
  38.                           .Data("filterState");  
  39.                   })  
  40.                   .ServerFiltering(true);  
  41.               })  
  42.               .Enable(false)  
  43.               .AutoBind(false)  
  44.               .CascadeFrom("Country")  
  45.     )  
  46.     <script>  
  47.             function filterState() {  
  48.                 return {  
  49.                     Country: $("#Country").val()  
  50.                 };  
  51.             }  
  52.     </script>  
  53.   
  54.     <h4 style="margin-top: 2em;">City</h4>  
  55.     @(Html.Kendo().DropDownList()  
  56.               .Name("City")  
  57.               .HtmlAttributes(new { style = "width:100%" })  
  58.               .OptionLabel("Select city...")  
  59.               .DataTextField("City_Name")  
  60.               .DataValueField("City_ID")  
  61.               .DataSource(source =>  
  62.               {  
  63.                   source.Read(read =>  
  64.                   {  
  65.                       read.Action("GetCityList", "CascadingCountry")  
  66.                           .Data("filterCity");  
  67.                   })  
  68.                   .ServerFiltering(true);  
  69.               })  
  70.               .Enable(false)  
  71.               .AutoBind(false)  
  72.               .CascadeFrom("State")  
  73.     )  
  74.     <script>  
  75.             function filterCity() {  
  76.                 return {  
  77.                     State: $("#State").val()  
  78.                 };  
  79.             }  
  80.     </script>  
  81.   
  82. </div>  
  83.   
  84. <style>  
  85.     .k-readonly {  
  86.         color: gray;  
  87.     }  
  88. </style>  
Result



Initially while the view loads the GetCountryList, Action fires and the list of countries will be returned as JSON response, which is populated in Country dropdownlist.

 

While selecting the country from the country dropdownlist, it will fire GetStateList Action and the list of state will be returned as JSON response, based on the country value, which is passed as the query string parameters.



While selecting the State from State dropdownlist, it will fire GetCityList Action and the list of city will be returned as JSON response,
based on the state value, which is passed as query string parameters.
 
Source Code

I hope you enjoyed this article. Your valuable feedback, questions or comments about this article are always welcome.