zoukankan      html  css  js  c++  java
  • Html.DropDownListFor的用法总结

    在ASP.NET MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值。

    注:重点是要将DropDownList的数据源转换成IEnumerable<SelectListItem>类型的结合;

      SelectListItem类在System.Web.MVC命名空间下,其成员如下:

    • public bool Disabled { get; set; }
    • public SelectListGroup Group { get; set; }
    • public bool Selected { get; set; }
    • public string Text { get; set; }
    • public string Value { get; set; }
    1 //处理State,将IEnumerable<State>集合转换为IEnumerable<SelectListItem>集合
    2 public IEnumerable<SelectListItem> GetStates()
    3 {
    4         return _usStateService.GetStates().OrderBy(c => c.StateAbbreviation).Select(state => new SelectListItem
    5                                                                {
    6                                                                    Text = state.StateAbbreviation,
    7                                                                    Value = state.StateAbbreviation
    8                                                                });
    9 }

    使用如下示例:

    • 首先我们要定义一个Model,用户在DropDownList中选择指定的值付给属性ReadyTimeHour  
    1 public class EricSunModel
    2 {
    3     public string ReadyTimeHour { get; set; }
    4 }
    • Model定义完毕之后,接下来处理Controller的逻辑
    • 这里使用ViewData来记录DropDownList中所要显示的所有列表数值
     1 public ActionResult EricSunAction()
     2 {
     3     EricSunModel esModel = new EricSunModel();
     4     esModel.ReadyTimeHour = "00";
     5 
     6     GenerateReadyTimeViewData();
     7 
     8     return View(esModel);
     9 }
    10 
    11 private void GenerateReadyTimeViewData()
    12 {
    13     ViewData["HourList"] = GetTimeHourList();
    14 }
    15 
    16 //这里要对集合进行处理,因为第二个参数必须是IEnumerable<SelectListItem> 类型的集合
    17 private List<SelectListItem> GetTimeHourList()
    18 {
    19     List<SelectListItem> hourList = new List<SelectListItem>();
    20 
    21     for (int i = 0; i < 24; i++)
    22     {
    23         if (i < 10)
    24         {
    25             hourList.Add(new SelectListItem { Text = "0" + i.ToString(), Value = "0" + i.ToString() });
    26         }
    27         else
    28         {
    29             hourList.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
    30         }
    31     }
    32 
    33     return hourList;
    34 }
    • 接下来我们在View中可以用下面一行代码来绑定DropDownList
    • 注:第一个参数为绑定Model中的属性,即-->要为此属性赋值
    • 注:第二个参数为DropDownList的所有数据源,并且必须是IEnumerable<SelectListItem>类型的集合

    1 @Html.DropDownListFor(m => m.ReadyTimeHour, ViewData["HourList"] as List<SelectListItem>)
    • 效果如下

    • 如果我们想在DropDownList中的最顶端添加一个默认值的话,添加第三个参数
    1 @Html.DropDownListFor(m => m.ReadyTimeHour, ViewData["HourList"] as List<SelectListItem>, "---Select---")

  • 相关阅读:
    99. 恢复二叉搜索树
    337. 打家劫舍 III(dp+dfs)
    45. 跳跃游戏 II
    贪心
    460. LFU 缓存
    213. 打家劫舍 II(dp)
    MyScript 开发文档
    Android 全面屏体验
    Android Studio 导入自己编译的 framework jar
    cmake常用配置项
  • 原文地址:https://www.cnblogs.com/sunshineground/p/4372538.html
Copyright © 2011-2022 走看看