Search code examples
asp.net-mvcentity-frameworkmany-to-many

ASP.NET MVC, 3 static dropdownlists. It should parse all the values at once, but it doesn't.


Hi guys sorry for the short post ;0)

I have a view with tre dropdownlists, then i sumbit the form it should parse all the selected values at once to the database, but it doesn't.

Here is my code:

the view....

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>SaveRatings</title>
</head>
<body>
    <div> 

        @using (Html.BeginForm("SaveRatings", "Scout"))
        {
            int id = 3;

            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            for (int i = 0; i < id; i++)
            {
                @Html.DropDownList("Value", (IEnumerable<SelectListItem>)ViewBag.Value, "Vælg en værdi fra 1 - 20", new { @class = "form-control" })<br/>
            }






            <input type="submit" value="Save" />

        }


    </div>
</body>
</html>

the controller and the ValueDropDownList method

   public ActionResult SaveRatings()
        {
            ValueDropDownList();
            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SaveRatings(string Value)
        {

            using (Connection db = new Connection())
            {
                List<Rating> rt = new List<Rating>();

                rt.Add(new Rating
                {
                    Value = Value
                });

                //For every obejct in the list, it should add the items....
                foreach (var item in rt)
                {

                    db.Ratings.Add(item);

                }



                db.SaveChanges();


            }
            return View("GetProperties");

        }

The ValueDropDownList method

 public void ValueDropDownList()
    {
        List<SelectListItem> valueitems = new List<SelectListItem>();

        valueitems.Add(new SelectListItem { Text = "1", Value = "1" });

        valueitems.Add(new SelectListItem { Text = "2", Value = "2" });

        valueitems.Add(new SelectListItem { Text = "3", Value = "3" });

        ViewBag.Value = valueitems.ToList();
    }

Solution

  • Your current view code is going to render 3 SELECT element with the name "Value". When you submit the form, the form data will have 3 keys with the name "Value" and each ones value is your selected option value.

    Now since it is sending an array (because all three dropdown names are same), you need to udpate your action method parameter to an array. Then you can loop through this array and save it.

    public ActionResult SaveRatings(string[] Value)
    {
        using (var db = new Connection())
        {
            foreach (var item in Value)
            {
                var t=new Rating
                {
                    Value = item
                };
                db.Ratings.Add(t);                    
            }
            db.SaveChanges();
    
        }
        return RedirectToAction("Index");
    }