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 Jun 27, 2017 06:34 AM
Hi @sandip.AP,
Please try the following

Insert DropDownList Selected Value into Database using C# in ASP.Net

It might help you.

Cheers Andrea.

Posted on Jun 27, 2017 06:39 AM

Hi Andrea Actually this is For C#.net Only this is not Usefull to me, please share same as MVC

 



Posted on Jul 08, 2017 05:50 AM

Dear Anand Getting very good Solution, but the thing is that i want to store this value into Database and Retrive it when it Edit.

 

and this is limited to Alert Message Only.

 

please share the Code if you have with my requirement.

 

Thanks ,

Sandip Patil


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.

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