Html.DropDownList()赋默认值:

页面代码如下:

  <% 
List
<SelectListItem> list = new List<SelectListItem> {
new SelectListItem { Text = "启用", Value = "0",Selected = true},
new SelectListItem { Text = "禁用", Value = "1" } };
%>//list储存dropdownlist的默认值
<%=Html.DropDownList("state",list,Model.state) %> //state为实体的属性,默认选中"启用"

 


Html.DropDownList()从数据库读取值:

页面代码如下:

<%= Html.DropDownList("Category", ViewData["Categories"] as SelectList,"--请选择--",new { @class = "my-select-css-class" } )%>


Controllers代码:

public ActionResult Create() 
{
List<Category> categories = categoryService.GetAll();
ViewData["Categories"] = new SelectList(categories, "Id", "Name");
return View();
}

 

相关文章:

  • 2021-05-27
  • 2021-10-25
  • 2021-07-28
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2022-02-21
猜你喜欢
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2021-11-08
  • 2022-12-23
相关资源
相似解决方案