如何给DropDownListFor设置默认值
1、直接赋值:
@Html.DropDownListFor(o => o.ParentId, ViewBag.root as IEnumerable<SelectListItem>,"默认值")
2、在初始化时赋值:
View层:
@Html.DropDownListFor(o => o.ParentId, ViewBag.root as IEnumerable<SelectListItem>)
Controller层:
var parent = _dictionaryService.Where(o => o.IsDeleted == false && o.DisLevel == 1) .ToList() .Select(o => new SelectListItem() { Text = o.Name.ToString(), Value = o.Id.ToString() }); ViewBag.root = parent; DictionaryViewModel viewModel = new DictionaryViewModel(); viewModel.ParentId = id; string disOrder = ""; disOrder = LoadDetailOrderNumber(id); viewModel.DisOrder = disOrder.AsInt(); return View(viewModel);