使用正在编辑表单中工作的ViewBag创建了下拉列表。如何在下拉列表中添加“从列表中选择”,目前它显示一个空格? 在哪里放“从列表中选择” - 控制器或视图。
答案 0 :(得分:1)
DropDownList / DropDownListFor帮助器有一个重载,它允许你添加一个默认值:
@Html.DropDownListFor(
x => x.SelectedId,
(IEnumerable<SelectListItem>)ViewBag.Items, // <= yuck, use a view model instead of ViewBag/ViewCrap
"select from list"
)