0

I am trying to create an editable list with a dropdown.

The SelectList doesn't select the value given in Device_Parts. It gives the first value of pricelist.

Controller:

var Device_Parts = (from dp in db.Device_Parts
                                    join dc in db.DeviceConfig on dp.Device_config_id equals dc.Device_config_id
                                    where dp.Device_config_id == Id
                                    select dp).ToList();

                var SelectedCMI = (from Item in db.pricelist
                                   select new { Id = Item.Price_id, name = Item.bas_art_nr });
                ViewData["SelectedCMI"] = new SelectList(SelectedCMI, "Id", "name");

Part of view:

  @Html.DropDownListFor(a => a[j].Price_id, (IEnumerable<SelectListItem>)ViewData["SelectedCMI"], new { onchange = "Action(this.value);", @class = "form-control", @id = "code" })

What am I doing wrong?

Roland_dfa
  • 48
  • 10
  • well...those answers didn't help – Roland_dfa Jul 26 '18 at 11:01
  • Then you did not implement it! Read it again carefully - you need to use an `EditorTemplate` or you need to create a new `SelectList` in each iteration of your loop and set the `Selected` property –  Jul 26 '18 at 11:03
  • @StephenMuecke this is helping me out: https://stackoverflow.com/questions/37002971/mvc-dropdown-list-with-model-of-type-list/37048997#37048997 – Roland_dfa Jul 27 '18 at 09:45

0 Answers0