ViewBag.TxtCode=new SelectList(_db.table,"ValueField","TextField");
@Html.DropDownListFor(d=>d.id,ViewBag.TxtCode as SelectList,"--Please Select--",new{Class="dropclass"})
特定对象集合:
ViewBag.ProCode = new SelectList(_db.Products.Select(p => new { p.CodePro, protext = p.ProClass.Name + " | " + p.Material.Name + " | Φ" + p.Spec.Name}), "CodePro", "protext");
@Html.DropDownList通常用在不需要绑定Model字段比如:
@Html.DropDownList("proid", ViewBag.DProvince as SelectList, "请选择")
好处是其ID值可以随意写,提交值如果是空也不影响提交,不会进行验证。
@Html.DropDownListFor通常用在绑定Model字段里
学习交流群:364976091