如何给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);

  

posted @ 2015-06-13 17:43  小个子猫  阅读(5989)  评论(0编辑  收藏  举报