user3793029 user3793029 - 3 months ago 17
ASP.NET (C#) Question

How to insert dynamically generated dropdownlist value into DB by using MVC

Need help in MVC : Please help in Creating dropdownlist(child) based on the count from a dropdownlist selected value(integer)- consider it as a parent control. And insert the child dropdownlist selected values to the database using MVC Eg; If 3 is selected in parent dropdownlist,3 new dropdownlist needs to be created and selected values of 3 dropdownlist needs to be inserted into DB--By using MVC dropdownlist . While I tried,only first child dropdownlist selected value is getting inserted or three times..Please help in resolving it

Ali Ali
Answer

First Creating parent dropdownlist. Starting with Home Controller, i create a list

public ActionResult Index()
    {
        List<int> key =new List<int>();
        key.Add(1); key.Add(2); key.Add(3); key.Add(4); key.Add(5);
        ViewBag.RequiredKey = new SelectList(key);
        return View();
    }

In the Index View i show the parent dropdownlist

 @using (Html.BeginForm("SelectedDropDownResult", "Home",FormMethod.Post))
    {
        @Html.DropDownList("SelectedDropDownValue", (SelectList)ViewBag.RequiredKey, new { @class = "form-control" })
        <input type="submit" value="Submit">
    }

Here in this dropdownlist user selects a value which is posted to the action named SelectedDropDownResult in the Home controller

 public ActionResult SelectedDropDownResult(FormCollection fc)
        {
            int dropDown = int.Parse(fc["SelectedDropDownValue"]);
            ViewBag.dropDownValue = dropDown;

            List<int> key = new List<int>();
            key.Add(1); key.Add(2); key.Add(3); key.Add(4); key.Add(5);
            ViewBag.RequiredKey = new SelectList(key);

            return View();
        }

Using FormCollection lets extract the user selected value in parent drop down

 @{
        ViewBag.Title = "SelectedDropDownResult";
    }

    <h3> Generating @ViewBag.dropDownValue based on parent drop down selected value</h3>

    @using (Html.BeginForm("ChildDropDown", "Home", FormMethod.Post))
    {
        <input type="hidden" name="childDropDownValue" value=@ViewBag.dropDownValue>

        for (int i=0; i< @ViewBag.dropDownValue;i++ )
        { 
        @Html.DropDownList("SelectedDropDownValue"+i, (SelectList)ViewBag.RequiredKey, new { @class = "form-control" })
        }
        <input type="submit" value="Submit">
      }

Here child drop down lists are created based on the count of parent list and action ChildDropDown is called to save data to database

 public ActionResult ChildDropDown(FormCollection fc)
        {
            List<int> child=new List<int>();
            int dropDown = int.Parse(fc["childDropDownValue"]);
            for(int i=0;i<dropDown;i++)
            { 
              child.Add(int.Parse(fc["SelectedDropDownValue"+i]));
            }

            // code to add data child list to the database

            return View();

        }

    }

You can now add code to save data to the database in ChildDropDown action of Home controller

Comments