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字段里

相关文章:

  • 2022-12-23
  • 2021-06-01
  • 2021-11-28
  • 2021-12-21
  • 2021-12-15
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-20
  • 2021-06-10
  • 2021-12-12
  • 2021-12-11
  • 2021-05-30
相关资源
相似解决方案