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

Last Reply 2 months ago By AnandM

Posted 3 months ago

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 2 months ago

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.

[HttpPost]
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);
}

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html