How to Insert multiple DropDownList selected value in Database using C# in ASP.Net MVC

Last Reply on Jul 10, 2017 08:44 AM By AnandM

Posted on Jun 27, 2017 06:34 AM

Hi Guys, 


I Am Creating on Project Name As DPR.


Now within that project one page name as Project.


now within This Project I Have values name as Project Code, Location and Activity.


Now Project Code and Location is in Project Table and Activity is from Activity Table.


The Question is I Want to Store Multiple Activity Using Multiselect Drodown list in Database using MVC.


How to Do this, i have lots of Searching but nver get the Answer.


Please help me.

Posted on Jul 10, 2017 08:44 AM

Hi sandip.AP,

The article just shows how you can get multiple selected items in code and what you need to do is once you get multiple selected items then insert in those selected items text or value in database like below.

Please read the comments section carefully.

public ActionResult Index(FruitModel fruit)
    fruit.Fruits = PopulateFruits();
    if (fruit.FruitIds != null)
        List<SelectListItem> selectedItems = fruit.Fruits.Where(p => fruit.FruitIds.Contains(int.Parse(p.Value))).ToList();

        string allSelectedItem = "";
        foreach (var selectedItem in selectedItems)
            //Here you need to write the insert query for saving the value in database but it will add in row in sql table.
            string singleSelectedItem = selectedItem.Text;                

            //if you want to save in single row with comma separated then you need to add the values with comma separated like below.
            allSelectedItem += selectedItem.Text + ",";
        //Insert query comes here for comma separated values.
        allSelectedItem.Remove(allSelectedItem.Length - 1);
    return View(fruit);