为视图生成下拉框方法
//1.为视图生成下拉框方法准备SelectListItem集合 ViewBag.listItem=db.BlogArticleCates.Where(c=>c.IsDel==false).ToList().Select(c => new SelectListItem() { Value = c.Id.ToString(), Text = c.Name }); //交给EF的LINQ不能包含非SQL语法,但是集合类型的LINQ可以包含非SQL语法(如:tostring()) //2.使用 SelectList 来准备 下拉框方法 所需的 数据 List<Models.BlogArticleCate> list = db.BlogArticleCates.Where(c => c.IsDel == false).ToList(); ViewBag.selList = new SelectList(list,"Id","Name"); ViewBag.statuList = new SelectList(db.Enumerations.Where(e => e.e_isDel == false && e.e_type == "3").ToList(), "e_id", "e_cname");
@*@Html.DropDownListFor(a=>a.ACate,ViewBag.listItem as IEnumerable<SelectListItem>)*@ @Html.DropDownListFor(a=>a.ACate,(ViewBag.selList as SelectList).ToList())