1

I am trying to provide a UI to edit multiple object configurations using Razor. The objects are passed into the page as an array. The number of objects is variable, so in the Razor view I am using a foreach loop to create rows in a table, where each row element contains a DropDownListFor element for which the value is bound to each object property, i.e., an array element. The available values are passed in the ViewBag.

Any example of one of the DropDownListFor instances is:

@Html.DropDownListFor(m => m.DeviceConfig.Devcfg.Relays[i].RSDLY, new SelectList(ViewBag.Delays))

In this can ViewBag.Delays is defined as:

ViewBag.Delays = Enumerable.Range(0, 9).ToArray();

What I am seeing is that when the page is first loaded, the drop downs DO NOT initialize to the value stored in the object array that it is bound to. If I make a change to the selected value in any of the drop downs, and POST the form back to the server, the new values have indeed been captured.

In short, when the page first loads, the initial selected value is not being taken from the model, but when a change is made via the UI, the value is reflected in the model.

Note that ViewBag.Delays are integers, as are the object properties.

ProfNimrod
  • 4,142
  • 2
  • 35
  • 54

0 Answers0