如何给DropDownListFor设置默认值
1个回答
展开全部
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()
});
<span style="color: #0000ff;"><strong>ViewBag.root = parent;</strong></span>
<span style="color: #0000ff;">DictionaryViewModel viewModel = new DictionaryViewModel();
viewModel.ParentId = id;</span>
string disOrder = "";
disOrder = LoadDetailOrderNumber(id);
<span style="color: #0000ff;">viewModel.DisOrder = disOrder.AsInt();
return View(viewModel);
</span>
@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()
});
<span style="color: #0000ff;"><strong>ViewBag.root = parent;</strong></span>
<span style="color: #0000ff;">DictionaryViewModel viewModel = new DictionaryViewModel();
viewModel.ParentId = id;</span>
string disOrder = "";
disOrder = LoadDetailOrderNumber(id);
<span style="color: #0000ff;">viewModel.DisOrder = disOrder.AsInt();
return View(viewModel);
</span>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询