Recent problems in the code thing, encountered a simple drop-down box functionality, with the following method
@Html.DropDownListFor()
Which requires a IEnumerable<SelectListItem>
class passed as a parameter data, bloggers used herein ViewData
, the following specific code
<div class="col-sm-8">
@Html.DropDownListFor(t => t.Container, ViewData["Container"] as IEnumerable<SelectListItem>, new { @class = "form-control", @style = "font-size:13px" })
@Html.ValidationMessage("Container", new { @class = "text-danger pull-right" })
</div>
Setting ViewData
data are as follows, controller
that the current controller, selectListItems
is a SelectListItem
collection of
List<SelectListItem> selectListItems = new List<SelectListItem>();
......
controller.ViewData["Container"] = selectListItems;
Wherein selectListItems
the following data, the second 'container two "dimension selected item
, but in a view, and no item is selected, as shown in FIG.
But then I might be ViewData
the name of the same name with the previous expression, leading to conflict
have tried to modify ViewData
the key
value
controller.ViewData["ContainerDDL"] = selectListItems;
------------------
@Html.DropDownListFor(t => t.Container, ViewData["ContainerDDL"] as IEnumerable<SelectListItem>, new { @class = "form-control", @style = "font-size:13px" })
Actually solve the problem. . . . .
However, it is unclear why this is so. = _ =!