【问题标题】:Dictionary Keys and Values to a Select List选择列表的字典键和值
【发布时间】:2020-08-30 16:21:04
【问题描述】:
Dictionary<string,string> dict = new Dictionary<string,string>();    
  dict.add("a1", "Car");
  dict.add("a2", "Van");
  dict.add("a3", "Bus");

SelectList SelectList = new SelectList((IEnumerable)mylist, "ID", "Name", selectedValue);

在上面的代码中,我将列表mylist 放入SelectListIDName 是该特定对象 list(mylist) 的两个属性。

同样我需要将字典添加到SelectList.


需要将字典的key添加到data Value参数-(上例ID位置) 需要将字典的值添加到data text参数-(上例的Name位置)

所以请告诉我一种使用此字典键和值创建选择列表的方法,而无需创建新类。

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    你可以试试:

    SelectList SelectList = new SelectList((IEnumerable)dict, "Key", "Value", selectedValue);
    

    Dictionary&lt;string, string&gt; 实现IEnumerable&lt;KeyValuePair&lt;string, string&gt;&gt;KeyValuePair 为您提供KeyValue 属性。

    但是请注意,通过枚举 Dictionary&lt;string,string&gt; 返回的项目的顺序并不能保证。如果您想要有保证的订单,您可以执行以下操作:

    SelectList SelectList = new SelectList(dict.OrderBy(x => x.Value), "Key", "Value", selectedValue);
    

    【讨论】:

    • 简短、直接、合理。很好的答案!
    【解决方案2】:

    您真正需要做的就是将字典作为参数传递并使用重载:

    public SelectList(IEnumerable items, string dataValueField, string dataTextField);
    

    例子:

    var dictionary = new Dictionary<string, string>
    {
       {"a1", "Car"}, 
       {"a2", "Van"}, 
       {"a3", "Bus"}
    };
    
    var selectList = new SelectList(dictionary, "Key", "Value");
    

    我知道帖子有点旧,但我来这里是为了找到答案,并根据之前给出的答案得出这个结论。

    【讨论】:

      【解决方案3】:

      您可以从 Dictionary 中构造一个 SelectListItem 对象列表,然后从中创建一个 SelectList。

      var dict = new Dictionary<string, string>
      {
         {"a1", "Car"}, 
         {"a2", "Van"}, 
         {"a3", "Bus"}
      };
      
      var myListItems = new List<SelectListItem>();
      
      myListItems.AddRange(dict.Select(keyValuePair => new SelectListItem()
      {
          Value = keyValuePair.Key, 
          Text = keyValuePair.Value
      }));
      
      var myList = new SelectList(myListItems);
      

      【讨论】:

        【解决方案4】:

        我喜欢这样写:

        @Html.DropDownListFor(model => model.Delimiter,
                                                 new Dictionary<string, string>
                                                 {
                                                     {",", ", (Comma)"},
                                                     { ";", "; (Semicolon)"}
                                                 }.Select(x => new SelectListItem {Value = x.Key, Text = x.Value}))
        

        因为这样,您不必依赖字符串"Key""Value"

        【讨论】:

          【解决方案5】:

          我将它用于 SelectListItem 列表,它适用于选择标签和下拉列表

          dict.OrderBy(x => x.Value).Select(r => new SelectListItem(r.Key, r.Value));
          

          【讨论】:

          • 注意 SelectListItem 构造函数中键和值的顺序。首先是显示文本,其次是从 SelectList 发送的“值”。来自字典,您可能希望“r.Key”成为 SelectList 值,而“r.Value”是显示文本。即r =&gt; new SelectListItem(r.Value, r.Key)
          【解决方案6】:

          试试

          SelectList SelectList = new SelectList((IEnumerable)mylist, "Key", "Value", selectedValue);
          

          【讨论】:

            猜你喜欢
            • 2016-01-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-03
            相关资源
            最近更新 更多