有时候,我们在输出数据的select列表的时候,还想添加一个默认选项,如 ”–请选择–“ 等,在MVC3.0中是这样实现的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
IEnumerable<SelectListItem> ColumnList = from a
in
Company.Column.ToList()
where a.parentID==0
select
new
SelectListItem
{
Value = a.ColumnID.ToString(),
Text = a.name
};
List<SelectListItem> Items =
new
List<SelectListItem>();
Items.Add(
new
SelectListItem { Value =
"0"
, Text =
"一级栏目"
});
Items.AddRange(ColumnList);
ViewBag.ColumnList = Items;
return
View();
|
视图中的代码:
1
|
@Html.DropDownList(
"parentID"
, (IEnumerable<SelectListItem>)ViewBag.ColumnList)
|