本文目录
- MVC中,Html.DropDownListFor怎么绑定数据
- mvc asp.net @Html.DropDownList如何实现滚动条呀,代码见详细描述,谢谢
- .net MVC 泛型类 绑定到 html.dropdownlist
MVC中,Html.DropDownListFor怎么绑定数据
①.在Action中查询要绑定到DropDownListFor的数据,并赋值给ViewBag:程序代码public ActionResult Create(){ ViewBag.Categories = new SelectList(db.Category, "Id", "Name"); return View();}②.在View中将数据绑定到DropDownListFor:程序代码@Html.DropDownListFor(model =》 model.CategoryId, ViewBag.Categories as IEnumerable《SelectListItem》);2.DropDownListFor选中指定项这需要在SelectList构造函数中设置,例如:程序代码public ActionResult Create(){ ViewBag.Categories = new SelectList(db.Category, "Id", "Name", "2"); return View();}
mvc asp.net @Html.DropDownList如何实现滚动条呀,代码见详细描述,谢谢
@Html.DropDownList("ThisYear", ViewBag.YearSelectList as IEnumerable《SelectListItem》,new {size=5})
《select id="ThisYear" name="ThisYear" size="5"》
超过 5 个就会出现滚动条,但是也有一个问题,固定了下拉框的 size 是 5,如果元素个数小于 Size,就会变成这样
希望对您有帮助
.net MVC 泛型类 绑定到 html.dropdownlist
Controllers文件中ViewData = new SelectList(ProjectList.AsEnumerable(), "ProjectID", "ProjectName", projectID);ProjectList是你从model entity取出来的一个普通的list。ProjectID,ProjectName是dropdownlist的value值和text的值。View文件中选择项目名称 《%=Html.DropDownList("Project", "-请选择-") %》这样就可以直接生成dropdownlist