Multiple-selection is an important part of drop-down UI component as it improves the user interactivity with the website in order to allow the user to make his/her choice first and then send the request to the server for batch processing instead of again and again sending a request to the server for same choice selection. One of the cool things about the Bootstrap CSS framework is that it provides very rich and interactive built-in plugins which are easy to use and integrate with any server-side technology.
Today, I shall be demonstrating the integration of the Bootstrap CSS style drop-down with the enabling of multi-selection choice by using Bootstrap Select plugin into ASP.NET MVC5 platform.

Prerequisites
Following are some prerequisites before you proceed any further in this tutorial.
- Knowledge of ASP.NET MVC5.
- Knowledge of HTML.
- Knowledge of JavaScript.
- Knowledge of Bootstrap.
- Knowledge of Jquery.
- Knowledge of C# Programming.
You can download the complete source code for this tutorial or you can follow the step by step discussion below. The sample code is being developed in Microsoft Visual Studio 2015 Enterprise. I am using Country table data extract from the Adventure Works Sample Database.
Let's begin now.Step 1
Step 2
Step 3
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>@ViewBag.Title</title>
- @Styles.Render("~/Content/css")
- @Scripts.Render("~/bundles/modernizr")
- <!-- Font Awesome -->
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" />
- </head>
- <body>
- <div class="navbar navbar-inverse navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- </div>
- </div>
- </div>
- <div class="container body-content">
- @RenderBody()
- <hr />
- <footer>
- <center>
- <p><strong>Copyright © @DateTime.Now.Year - <a href="http://wwww.asmak9.com/">Asma's Blog</a>.</strong> All rights reserved.</p>
- </center>
- </footer>
- </div>
- @*Scripts*@
- @Scripts.Render("~/bundles/jquery")
- @Scripts.Render("~/bundles/jqueryval")
- @Scripts.Render("~/bundles/bootstrap")
- @RenderSection("scripts", required: false)
- </body>
- </html>
In the above code, I have simply created a basic default layout page and linked the require libraries into it.
Step 4
- //-----------------------------------------------------------------------
- // <copyright file="CountryObj.cs" company="None">
- // Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.
- // </copyright>
- // <author>Asma Khalid</author>
- //-----------------------------------------------------------------------
- namespace SaveMultiSelectDropDown.Helper_Code.Objects
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- /// <summary>
- /// Country object class.
- /// </summary>
- public class CountryObj
- {
- #region Properties
- /// <summary>
- /// Gets or sets country ID property.
- /// </summary>
- public int Country_Id { get; set; }
- /// <summary>
- /// Gets or sets country name property.
- /// </summary>
- public string Country_Name { get; set; }
- #endregion
- }
- }
In the above code, I have simply created an object class which will map my sample list data in order to populate the drop-down list.
Step 5
- //-----------------------------------------------------------------------
- // <copyright file="MultiSelectDropDownViewModel.cs" company="None">
- // Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.
- // </copyright>
- // <author>Asma Khalid</author>
- //-----------------------------------------------------------------------
- namespace SaveMultiSelectDropDown.Models
- {
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Web;
- using Helper_Code.Objects;
- /// <summary>
- /// Multi select drop down view model class.
- /// </summary>
- public class MultiSelectDropDownViewModel
- {
- #region Properties
- /// <summary>
- /// Gets or sets choose multiple countries property.
- /// </summary>
- [Required]
- [Display(Name = "Choose Multiple Countries")]
- public List<int> SelectedMultiCountryId { get; set; }
- /// <summary>
- /// Gets or sets selected countries property.
- /// </summary>
- public List<CountryObj> SelectedCountryLst { get; set; }
- #endregion
- }
- }
In the above code, I have created my view model which I will attach with my view. Here, I have created integer type list property which will capture my multiple selection values from the Razor View drop-down control and country object type list property which will display my multiple-selection choice in a table after processing on the server via ASP.NET MVC5 platform.
Step 6
- //-----------------------------------------------------------------------
- // <copyright file="MultiSelectDropDownController.cs" company="None">
- // Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.
- // </copyright>
- // <author>Asma Khalid</author>
- //-----------------------------------------------------------------------
- namespace SaveMultiSelectDropDown.Controllers
- {
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Web;
- using System.Web.Mvc;
- using Helper_Code.Objects;
- using Models;
- /// <summary>
- /// Multi select drop down controller class.
- /// </summary>
- public class MultiSelectDropDownController : Controller
- {
- #region Index view method.
- #region Get: /MultiSelectDropDown/Index method.
- /// <summary>
- /// Get: /MultiSelectDropDown/Index method.
- /// </summary>
- /// <returns>Return index view</returns>
- public ActionResult Index()
- {
- // Initialization.
- MultiSelectDropDownViewModel model = new MultiSelectDropDownViewModel { SelectedMultiCountryId = new List<int>(), SelectedCountryLst = new List<CountryObj>() };
- try
- {
- // Loading drop down lists.
- this.ViewBag.CountryList = this.GetCountryList();
- }
- catch (Exception ex)
- {
- // Info
- Console.Write(ex);
- }
- // Info.
- return this.View(model);
- }
- #endregion
- #region POST: /MultiSelectDropDown/Index
- /// <summary>
- /// POST: /MultiSelectDropDown/Index
- /// </summary>
- /// <param name="model">Model parameter</param>
- /// <returns>Return - Response information</returns>
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult Index(MultiSelectDropDownViewModel model)
- {
- // Initialization.
- string filePath = string.Empty;
- string fileContentType = string.Empty;
- try
- {
- // Verification
- if (ModelState.IsValid)
- {
- // Initialization.
- List<CountryObj> countryList = this.LoadData();
- // Selected countries list.
- model.SelectedCountryLst = countryList.Where(p => model.SelectedMultiCountryId.Contains(p.Country_Id)).Select(q => q).ToList();
- }
- // Loading drop down lists.
- this.ViewBag.CountryList = this.GetCountryList();
- }
- catch (Exception ex)
- {
- // Info
- Console.Write(ex);
- }
- // Info
- return this.View(model);
- }
- #endregion
- #endregion
- #region Helpers
- #region Load Data
- /// <summary>
- /// Load data method.
- /// </summary>
- /// <returns>Returns - Data</returns>
- private List<CountryObj> LoadData()
- {
- // Initialization.
- List<CountryObj> lst = new List<CountryObj>();
- try
- {
- // Initialization.
- string line = string.Empty;
- string rootFolderPath = this.Server.MapPath("~/Content/files/");
- string fileName = "country_list.txt";
- string fullPath = rootFolderPath + fileName;
- string srcFilePath = new Uri(fullPath).LocalPath;
- StreamReader sr = new StreamReader(new FileStream(srcFilePath, FileMode.Open, FileAccess.Read));
- // Read file.
- while ((line = sr.ReadLine()) != null)
- {
- // Initialization.
- CountryObj infoObj = new CountryObj();
- string[] info = line.Split(',');
- // Setting.
- infoObj.Country_Id = Convert.ToInt32(info[0].ToString());
- infoObj.Country_Name = info[1].ToString();
- // Adding.
- lst.Add(infoObj);
- }
- // Closing.
- sr.Dispose();
- sr.Close();
- }
- catch (Exception ex)
- {
- // info.
- Console.Write(ex);
- }
- // info.
- return lst;
- }
- #endregion
- #region Get country method.
- /// <summary>
- /// Get country method.
- /// </summary>
- /// <returns>Return country for drop down list.</returns>
- private IEnumerable<SelectListItem> GetCountryList()
- {
- // Initialization.
- SelectList lstobj = null;
- try
- {
- // Loading.
- var list = this.LoadData()
- .Select(p =>
- new SelectListItem
- {
- Value = p.Country_Id.ToString(),
- Text = p.Country_Name
- });
- // Setting.
- lstobj = new SelectList(list, "Value", "Text");
- }
- catch (Exception ex)
- {
- // Info
- throw ex;
- }
- // info.
- return lstobj;
- }
- #endregion
- #endregion
- }
- }




Nelson NunesPosted Apr 15, 2020, 5:08 AM
Hello Asma, you should put the snippet with the necessary changes in BundleConfig.cs in order to the following code work: @*Scripts*@ @Scripts.Render("~/bundles/bootstrap-select") @*Styles*@ @Styles.Render("~/Content/Bootstrap-Select/css")
Yogi SPosted Nov 26, 2018, 4:16 AM
Its a very lengthy article with lots of unnecessary things, which you can certainly remove.
Asma KhalidPosted Nov 23, 2018, 10:13 PM
Thank You for the support
Abdul AzizPosted Nov 23, 2018, 1:02 AM
Great Post
Sarathlal SaseendranPosted Nov 23, 2018, 12:59 AM
Very nice and useful article Asma. Thanks for sharing.