1、在MVC的View页面中使用DropDownListFor时当DropDownListFor是列表是通过后台ViewBag传过来时,当ViewBag中的Key与DropDownListFor一致时,选择项会始终在第一项,如:@Html.DropDownListFor(o => o.RoleType, ViewBag.RoleTypeas IEnumerable<SelectListItem>)(错误的写法)

2、正确的写法如下:

页面上:

 

@Html.DropDownListFor(o => o.RoleType, ViewBag.ddlroleType as IEnumerable<SelectListItem>)

 

后台Controller中:

 

var roleType = _dictionaryService.Where(o => o.IsDeleted == false && o.ParentId == roleTypeParent.Id)
                                 .ToList()
                                 .Select(o => new SelectListItem()
                                 {
                                     Text = o.Name.ToString(),
                                     Value = o.Id.ToString()
                                 });
ViewBag.ddlroleType = roleType;

 

相关文章:

  • 2021-06-09
  • 2022-01-20
  • 2021-06-22
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2021-08-13
  • 2021-05-30
相关资源
相似解决方案