ASP.NET MVC 5 - Kendo UI - Working With MultiColumnComboBox Using EF

Why do we want to learn this MultiColumnComboBox? Basically, most of the Dropdowns or Comboboxes help populate only one column of  data but this MultiColumnComboBox helps us to display multi-column data in a single row, like a grid with/without images. That's quite interesting, right? Okay, let's move forward to see these features with an example.

Prerequisites 
  • Visual Studio
  • SQL Server
  • Basic knowledge of ASP.NET MVC
  • Basic knowledge of Entity Framework
  • Basic knowledge of jQuery
  • Basic knowledge of CSS
Article Flow
  • Create a table in the database with dummy values
  • Create an ASP.NET MVC Empty project
  • Configure Entity Framework with database and application
  • Create a Controller and View
  • Enable Kendo UI Features
  • Implement MultiColumnComboBox
  • Custom Template with MultiColumnComboBox

Create a table in the database with dummy values

 
First, we will create a table in SQL Server to populate a Kendo UI MultiColumnComboBox with data in ASP.NET MVC5. I have created a table "Employee" with the following design. 
 
ASP.NET MVC 5 - Kendo UI - Working With MultiColumnComboBox Using EF 
 
Execute the below query to create a table with the above design.
  1. CREATE TABLE [dbo].[Employee](  
  2. [ID] [bigint] IDENTITY(1,1) NOT NULL,  
  3. [Name] [nvarchar](150) NULL,  
  4. [Country] [nvarchar](50) NULL,  
  5. [Role] [nvarchar](50) NULL,  
  6. [ImageName] [nvarchar](50) NULL,  
  7. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED  
  8. (  
  9.    [ID] ASC  
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  11. ON [PRIMARY]  
  12. GO  
And, insert a few employee records like below.
 
ASP.NET MVC 5 - Kendo UI - Working With MultiColumnComboBox Using EF 
 
Execute the below query to get the same records.
  1. USE [CSharpCorner]  
  2. GO  
  3. SET IDENTITY_INSERT [dbo].[Employee] ON  
  4. GO  
  5. INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (1, N'Gnanavel Sekar', N'India', N'Sr. Software Engineer', N'gnanavelsekar')  
  6. GO  
  7. INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (2, N'Gokul', N'UK', N'Sr. Software Engineer', N'ManIcon')  
  8. GO  
  9. INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (3, N'Ramar', N'India', N'Technical Lead', N'ManIcon')  
  10. GO  
  11. SET IDENTITY_INSERT [dbo].[Employee] OFF  
  12. GO  

Create an ASP.NET MVC Empty project

To create ASP.NET MVC empty project, follow the below steps one by one. Here, I have used Visual Studio 2017.
  • Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it as "KendoMultiColumnComboBox".
  • Now, click OK.
  • Then, select Empty MVC template and click OK to create the project.
  • Once you click OK, the project will be created with the basic architecture of MVC.
  • If you are not aware of how to create an Empty ASP.NET Web Application, please visit  Step 1 and Step 2  to learn. Once you complete these steps, you will get the screen as below.
ASP.NET MVC 5 - Kendo UI - Working With MultiColumnComboBox Using EF
 

Configure Entity Framework with database and application

 
Here, I have already discussed how to configure and implement a database first approach. In the meantime, choose our created table with entity framework. Once we finish our configuration with SQL table "Employee" from CSharpCorner database and with our application, we will get the below screen as the succeeding configuration.
 
 ASP.NET MVC 5 - Kendo UI - Working With MultiColumnComboBox Using EF

 
Create a Controller and View

 
Now, create an empty Controller and View. Here, I created a Controller with the name of "KendoUIController". Whenever we create an empty Controller, it is created with an empty Index action method. And, create an empty View of this action method "Index".
 
Enable Kendo UI Features
 
Here, we are going to enable the Kendo UI features with our application by adding the below CSS and JS in our shared _Layout or Index View.
  1. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.common-material.min.css" />  
  2. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.min.css" />  
  3. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.mobile.min.css" />  
  4. <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js"></script>  
  5. <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js"></script>  
Now, write the logic in a Controller to load the employee(s) from a database using Entity Framework. Add the below code to load it. 
  1. public ActionResult LoadEmployee()  
  2. {  
  3.    CSharpCornerEntities cSharpCornerEntities = new CSharpCornerEntities();  
  4.    var employees = cSharpCornerEntities.Employees.ToList();  
  5.    return Json(employees, JsonRequestBehavior.AllowGet);  
  6. }  

Implement MultiColumnComboBox

 
And now, create a div in the View which will act as MultiColumnComboBox, Here I have mentioned id div is employees 
  1. <div id="example" class="col-lg-3">  
  2.    <div class="demo-section k-content">  
  3.       <input id="employees" style="width: 100%;" />  
  4.    </div>  
  5. </div>  
Now, enable the Kendo UI MultiColumnComboBox features to div("employees")
  1. $("#employees").kendoMultiColumnComboBox();   
Let's call the LoadEmployee action method to bind the data to MultiColumnComboBox Datasource.
  1. $(document).ready(function() {  
  2.     dataSource = new kendo.data.DataSource({  
  3.         transport: {  
  4.             read: {  
  5.                 url: "/KendoUI/LoadEmployee",  
  6.                 dataType: "json",  
  7.             }  
  8.         },  
  9.     });  
  10. });  
Detailed Description
  • transport-> Kendo UI transport helps us to do the CRUD operation (that means read, update, delete and create).
  • read - > It is used to read the respective type data from mentioned url.
  • url -> kendo read takes a place to read the data from this mentioned url. Here, we metioned to read from LoadEmployee action under KendoUI controller
  • dataType:  What type of data it should support reading
Now, bind this dataSource to KendoMultiColumnComboBox.
  1. $("#employees").kendoMultiColumnComboBox({  
  2.     dataTextField: "Name",  
  3.     dataValueField: "ID",  
  4.     height: 400,  
  5.     columns: [{  
  6.         field: "Name",  
  7.         title: "Name",  
  8.         width: 120  
  9.     }, {  
  10.         field: "Role",  
  11.         title: "Role",  
  12.         width: 160  
  13.     }, {  
  14.         field: "Country",  
  15.         title: "Country",  
  16.         width: 90  
  17.     }],  
  18.     footerTemplate: 'Total #: instance.dataSource.total() # items found',  
  19.     filter: "contains",  
  20.     filterFields: ["Name""Country""Role"],  
  21.     dataSource: dataSource  
  22. });  
Detailed Description 
  •  $("#employees").kendoMultiColumnComboBox({ -> Enable kendoMultiColumnComboBox features to employees div
  • dataTextField: "Name", -> It represents what we want to display in dropdown, Name is the property/column name 
  • dataValueField: "ID", -> ID is the value field While selecting the dropdown value  
  • height -> Total height of the control
  • columns: [ -> It's to represent what are the field needs to be displayed, Here we mentioned the column names are Name, Role, and Country
  • field -> It represents which column data should appear here
  • title -> Our custom name or display name for the respective column
  • width - It represents the width of the respective column
  • footerTemplate-> To create Custom logic. Here we mentioned to display the total count of record. This always will appear in the bottom of the control
  • Instance.dataSource.Total() -> It helps to find the total count of record available in KendoMultiColumnComboBox data source
  • filter: "contains", -> There has many filters, like equal to, contains, etc.,  
  • filterFields: ["Name", "Country", "Role"],  -> filters only applied to these fields
  • dataSource: dataSource- > Jason Data loaded here 
Run your application.
 
 ASP.NET MVC 5 - Kendo UI - Working With MultiColumnComboBox Using EF
 

 Custom Template with MultiColumnComboBox

 
I have told you already that we do our own customization using the template. Here, we are going to display the employee name with the name and for that, I am going to store the images in server directory and store that name in the database table (You can view database dummy values in the second image).
 
 ASP.NET MVC 5 - Kendo UI - Working With MultiColumnComboBox Using EF
 
Now add the field in column multicolumncombobox with the template
  1. columns: [  
  2.    {  
  3.       field: "Name", title: "Emp Name", template: "<div class='customer-photo'" +  
  4.       "style='background-image: url(../Content/Images/#:data.ImageName#.jpg);'></div>" +  
  5.       "<span class='customer-name'>#: Name #</span>", width: 170  
  6.    },  
  7. ]  
Detailed Description
  • Template ->  to add our customization
  • #:data.ImageName# -> Loading the Imagename 
  • # -> We can read the datasource data using the # followed by the column name and end with the #
and add the CSS for the template.
  1. < style type = "text/css" >   
  2. .customer - photo  
  3.  {    
  4.       display: inline - block;    
  5.       box - shadow: inset 0 0 1 px #999, inset 0 0 10px rgba(0,0,0,.2);    
  6.       margin: 0 10px 0;    
  7.       width: 20px;    
  8.       height: 20px;    
  9.       border-radius: 50%;    
  10.       background-size: 100%;    
  11.       background-repeat: no-repeat;    
  12.       vertical-align: middle;    
  13.    }    
  14. </style>    
Run your application.
 
ASP.NET MVC 5 - Kendo UI - Working With MultiColumnComboBox Using EF
Complete View  
  1. @ {  
  2.     ViewBag.Title = "Index";  
  3.     Layout = "~/Views/Shared/_Layout.cshtml";  
  4. } < link rel = "stylesheet"  
  5. href = "https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.common-material.min.css" / > < link rel = "stylesheet"  
  6. href = "https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.min.css" / > < link rel = "stylesheet"  
  7. href = "https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.mobile.min.css" / > < script src = "https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js" > < /script> < script src = "https://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js" > < /script> < br / > < div id = "example"  
  8. class = "col-lg-3" > < div class = "demo-section k-content" > < input id = "employees"  
  9. style = "width: 100%;" / > < /div> < /div> < style type = "text/css" > .customer - photo {  
  10. display: inline - block;  
  11. box - shadow: inset 0 0 1 px #999, inset 0 0 10px rgba(0,0,0,.2);  
  12. margin: 0 10px 0;  
  13. width: 20px;  
  14. height: 20px;  
  15. border-radius: 50%;  
  16. background-size: 100%;  
  17. background-repeat: no-repeat;  
  18. vertical-align: middle;  
  19. }  
  20. </style>  
  21. <script>  
  22. $(document).ready(function () {  
  23. dataSource = new kendo.data.DataSource({  
  24.     transport: {  
  25.         read: {  
  26.             url: "/KendoUI/LoadEmployee",  
  27.             dataType: "json",  
  28.         }  
  29.     },  
  30. });  
  31. $("#employees").kendoMultiColumnComboBox({  
  32.     dataTextField: "Name",  
  33.     dataValueField: "ID",  
  34.     height: 400,  
  35.     columns: [{  
  36.             field: "ContactName",  
  37.             title: "Emp Name",  
  38.             template: "<div class='customer-photo'" + "style='background-image: url(../Content/Images/#:data.ImageName#.jpg);'></div>" + "<span class='customer-name'>#: Name #</span>",  
  39.             width: 170  
  40.         },  
  41.         //{ field: "Name", title: "Name", width: 120 },  
  42.         {  
  43.             field: "Role",  
  44.             title: "Role",  
  45.             width: 160  
  46.         }, {  
  47.             field: "Country",  
  48.             title: "Country",  
  49.             width: 90  
  50.         }  
  51.     ],  
  52.     footerTemplate: 'Total #: instance.dataSource.total() # items found',  
  53.     filter: "contains",  
  54.     filterFields: ["Name""Country""Role"],  
  55.     dataSource: dataSource  
  56. });  
  57. });   
  58. < /script>  
Complete Controller 
  1. using KendoMultiColumnComboBox.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace KendoMultiColumnComboBox.Controllers {  
  8.     public class KendoUIController: Controller {  
  9.         // GET: KendoUI  
  10.         public ActionResult Index() {  
  11.             return View();  
  12.         }  
  13.         public ActionResult LoadEmployee() {  
  14.             CSharpCornerEntities cSharpCornerEntities = new CSharpCornerEntities();  
  15.             var employees = cSharpCornerEntities.Employees.ToList();  
  16.             return Json(employees, JsonRequestBehavior.AllowGet);  
  17.         }  
  18.     }  
  19. }  
Refer to the attached project for reference. I have attached the demonstrated project without a package due to the size limit.
 

Summary 

 
In this article, we discussed how to work with MultiColumnComboBox in our ASP.NET MVC5 web application using Kendo UI and Entity Framework.
 
I hope it's helpful. Your valuable feedback and comments about this article are always welcome.


Similar Articles