In this article, I am going to explain you how to convert asp.net mvc dropdownlist into Multi Select Textbox using Chosen JS plugin. You can see in below figure how the dropdown will look like after converting into multi select textbox.
Creating ASP.NET MVC Empty Application
Now the next step is to create ASP.NET MVC empty application as shown below.
Go to File → New → Project. A new window will be open as shown below.
Now go to Web and select .NET Framework 4.5 and give project name and click on OK .
Now new window will open as shown below.
Now Select Empty Template, check on MVC checkbox and click on OK.
Now, a new project will be created as shown below.
Adding Models
Go to Models folder and add new class file CountryData.cs. Now replace all code with below code. Here we have one list property CountryList which is type of SelectListItem for holding list of country data. Four string property for holding Id of 4 different dropdowns which we will see in article.
Adding Controller
Next step is to add controller to application. Go to controller folder and add new empty controller named as Home controller. Replace all code with below code. Here we have 2 Action methods, first method is get method, it is used to return Country List data to view and second method is post method which has 4 input parameters of type string array. These parameter will store the selected country's ID of all 4 dropdowns from view to controller.
Installing Chosen using Nuget
Next step is to install Chosen JS. Right click on References and go to Manage NuGet Packages as shown below.
After that a new window will open as shown below. Search for Harvest.Chosen and install it.
After successful installation, you can see css file related to chosen is installed inside Content folder and chosen.jquery file is installed inside Scripts folder.
Adding View
Now, right click on home controller index action method, add new view and name Index. Select Empty template and uncheck Use layout page and click on Add as shown below.
Now go to Views / Home folder, open Index.cshtml file and replace all code with below code. Here first 3 files are jquery, chosen css and chosen.jquery files which is compulsory.There are 4 dropdownlist, first is normal drowdown, second is dropdown with bootstrap applied, third is normal dropdown with chosen without multi select and fourth drowdown is with multi select enable using Chosen.
To enable multi select, use id or class selector to attach chosen plugin as shown below and set multiple="true" property of dropdown(in 4th dropdown).