Implement Dropdown Multiselect With CheckboxList In MVC Using jQuery

Introduction

In this article, I am going to explain about how to bind the value to dropdown from the database and how to implement dropdown multiselect with checkboxlist, using jQuery multiselect plugin.

Knowledge Required 
  • Basic knowledge of MVC.
  • Basic knowledge of jQuery.
  • Basic knowledge of Entity Framework.
  • Basic knowledge of LINQ.
Article Flows
  1. Create database table with the values.
  2. Integrate Entity Framework.
  3. Create Controller, View and enable Multiselect plugin with dropdown.
  4. Bind the value to dropdown.
  5. Display selected value of dropdown.
  6. Create database table with the values.
Create a table, as shown below.

 

To create table given above, excute the query given below.
  1. CREATE TABLE [dbo].[Dropdown](  
  2. [Id] [int] IDENTITY(1,1) NOT NULL,  
  3. [Skill] [nvarchar](100) NULL,  
  4. CONSTRAINT [PK_Dropdown] PRIMARY KEY CLUSTERED  
  5. (  
  6. [Id] ASC  
  7. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  8. ON [PRIMARY]  
  9. GO  
I have inserted the value, as shown below.

 

To insert tabove values, execute SQL script given below.
  1. SET IDENTITY_INSERT [dbo].[Dropdown] ON  
  2. GO  
  3. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (1, N'C#.NET')  
  4. GO  
  5. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (2, N'ASP.NET')  
  6. GO  
  7. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (3, N'ASP.NET MVC')  
  8. GO  
  9. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (4, N'LINQ')  
  10. GO  
  11. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (5, N'ENTITY FRAMEWORK')  
  12. GO  
  13. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (6, N'WEB-API')  
  14. GO  
  15. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (7, N'DOCUSIGN')  
  16. GO  
  17. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (8, N'JQUERY')  
  18. GO  
  19. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (9, N'HTML5')  
  20. GO  
  21. INSERT [dbo].[Dropdown] ([Id], [Skill]) VALUES (10, N'SQLSERVER')  
  22. GO  
  23. SET IDENTITY_INSERT [dbo].[Dropdown] OFF  
  24. GO   
Integrate EntityFramework

I already discussed in a previous article about how to integrate Entity Framework with MVC. For this, refer to the two links given below.
  • http://www.c-sharpcorner.com/blogs/implement-database-first-approach-with-entity-framework 
  • http://www.c-sharpcorner.com/article/asynchronous-file-upload-using-kendo-ui-asynchronous-upload-with-mvc-and-entityf/ --visit step 2
Once you integrate the Entity Framework with your MVC Application, you will get the screenshot, as shown below.

 

Create Controller, View and enable Multiselect plugin with dropdown

I have created the Controller in the name of DropdownController under controller folder.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace DropdownMulitSelectCheckbox.Controllers {  
  7.     public class DropdownController: Controller {  
  8.         // GET: Dropdown  
  9.         public ActionResult Index() {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }  
To enable Multiselect plugin, we need to refer the script given below and CSS to our Application
  1. <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>  
  2. <link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css"  
  3. rel="stylesheet" type="text/css" />  
  4. <script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js"></script>  
  5. <link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css"  
  6. rel="stylesheet" type="text/css" />  
  7. <script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>  
Add HTML/ Razor control to creating dropdown. Here, I have created HTML. Select to create multiselect dropdown.
  1. <div> <select id="mySkills" multiple="multiple" required name="mySkillsName">  
  2. @if (ViewBag.mySkills != null)  
  3. {  
  4. foreach (var item in ViewBag.mySkills)  
  5. {  
  6. if (item.Text != null)  
  7. {  
  8. <option value="@item.Value">  
  9. @item.Text  
  10. </option>  
  11. }  
  12. }  
  13. }  
  14. </select> </div>  
Add the script given below to enable multiselect plugin to the respective control.
  1. <script type="text/javascript">  
  2.     $(function() {  
  3.         $('#mySkills').multiselect({  
  4.             includeSelectAllOption: true  
  5.         });  
  6.     });  
  7. </script>  
Finally, the View will be, as shown below.
  1. @ {  
  2.     ViewBag.Title = "Index";  
  3. } < script src = "http://code.jquery.com/jquery-1.12.4.min.js" > < /script> < link href = "http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css"  
  4. rel = "stylesheet"  
  5. type = "text/css" / > < script src = "http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" > < /script> < link href = "http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css"  
  6. rel = "stylesheet"  
  7. type = "text/css" / > < script src = "http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js" > < /script> < script type = "text/javascript" > $(function() {  
  8.     $('#mySkills').multiselect({  
  9.         includeSelectAllOption: true  
  10.     });  
  11. }); < /script> < h2 > Multiselect Dropdown with Checkbox < /h2> < div > < select id = "mySkills"  
  12. multiple = "multiple"  
  13. required name = "mySkillsName" > @if(ViewBag.mySkills != null) {  
  14.     foreach(var item in ViewBag.mySkills) {  
  15.         if (item.Text != null) { < option value = "@item.Value" > @item.Text < /option>  
  16.         }  
  17.     }  
  18. } < /select> < /div>   
Bind the value to dropdown

To bind the value to the dropdown, we need to get the value from the database and assign the value to the dropdown.



Get the value from the database, using an Entity Framework context
 

This ViewBag.mySkills value will be assigned to select (dropdown) Controller in View, as shown below. Check the database to see if it has the value or not. If there is no value in the database, it means that it will throw the null reference exception for which I checked the null condition.

 

Now, run the Application.

 

Display selected value of dropdown

To get the selected value from the dropdown, I am going to add one button and display the selected value on button click event in jQuery.
  1. <input type="button" id="btnGetSkills" value="Click me to Get Selected Skill(s)" />  
Proceed with the button click event in jQuery, as shown below.
  1. $("#btnGetSkills").click(function() {  
  2.     alert($("#mySkills").val());  
  3. });  
Now, run your Application and click button by selecting skills.

 
If you want to get the selected text change in View, write the code, as shown below.



Finally, it will be displayed, as shown below.
  1. @ {  
  2.     ViewBag.Title = "Index";  
  3. } < script src = "http://code.jquery.com/jquery-1.12.4.min.js" > < /script> < link href = "http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css"  
  4. rel = "stylesheet"  
  5. type = "text/css" / > < script src = "http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" > < /script> < link href = "http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css"  
  6. rel = "stylesheet"  
  7. type = "text/css" / > < script src = "http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js" > < /script> < script type = "text/javascript" > $(document).ready(function() {  
  8.     $(function() {  
  9.         $('#mySkills').multiselect({  
  10.             includeSelectAllOption: true  
  11.         });  
  12.     });  
  13.     $("#btnGetSkills").click(function() {  
  14.         alert($("#mySkills").val());  
  15.     });  
  16. }); < /script> < h2 > Multiselect Dropdown with Checkbox < /h2> < div > < select id = "mySkills"  
  17. multiple = "multiple"  
  18. required name = "mySkillsName" > @if(ViewBag.mySkills != null) {  
  19.     foreach(var item in ViewBag.mySkills) {  
  20.         if (item.Text != null) { < option value = "@item.Value" > @item.Text < /option>  
  21.         }  
  22.     }  
  23. } < /select> < /div> < br / > < br / > < input type = "button"  
  24. id = "btnGetSkills"  
  25. value = "Click me to Get Selected Skill(s)" / >  
Summary 

In this article, we learned how to implement dropdown with mulit select checkbox and get the selected checkbox value.

I hope it's helpful.

Up Next
    Ebook Download
    View all
    Learn
    View all