效果:
方法一:
View:
<div class="col-md-md-4"> <div class="input-group"> <span class="input-group-addon">部门:</span> @Html.DropDownList("f_GroupID", ViewData["ConsumerGroup"] as SelectList, "请选择",new { @class = "form-control" }) </div> </div>
注:"f_GroupID"相当于name="f_GroupID",我用的是KendoUI的Grid,所以name="f_GroupID"绑定的是kendo的schema的 f_GroupID
Controller:
List<Acc_Group> AG = sdt.Acc_Group.ToList(); SelectList select = new SelectList(AG, "f_GroupID", "f_GroupName"); ViewData["ConsumerGroup"] = select;
实体:
public partial class Acc_Group { public int f_GroupID { get; set; } public string f_GroupName { get; set; } public int f_GroupNO { get; set; } public int f_ParentId { get; set; } }
方法二:
View:
@Html.DropDownList("database",null, "请选择", new { @class = "form-control" })
Controller:
List<Acc_Group> AG = sdt.Acc_Group.ToList(); var selectItemList = new List<SelectListItem>(); { ", Text = "选择部门", Selected = true }; }; var selectList = new SelectList(AG, "f_GroupID", "f_GroupName"); selectItemList.AddRange(selectList); ViewBag.database = selectItemList;