• 如何将项目添加到在ASP.net MVC的选择列表


    业务场景是这样:项目中有查询字典表返回的是ienumerable,绑定到前台dropdownlist。需要在selectlist第一项加上“==请选择==”。

    网上搜了,大概有7种方法:

    1. 真的没有必要,除非你坚持的值为0做到这一点。该在HtmlHelper DropDownList的扩展允许您设置一个选项标签,显示了在选择一个空值的初始值.a个DropDownList的签名,有该选项的标签。
    
    <%= Html.DropDownList( "DropDownValue",
          (IEnumerable<SelectListItem>)ViewData["Menu"],
          "-- Select One --" ) %>
    
    
    2. 我得到这个通过填充一个SelectItemList,转换为一个列表,并在索引0添加一个值来工作。
    
    List<SelectListItem> items = new SelectList(CurrentViewSetups, "SetupId", "SetupName", setupid).ToList(); 
    items.Insert(0, (new SelectListItem { Text = "[None]", Value = "0" }));
    ViewData["SetupsSelectList"] = items;
    
    
    3. 
    
    public static SelectList IndividualNamesOrAll(this SelectList Object)
       {
        MedicalVarianceViewsDataContext LinqCtx = new MedicalVarianceViewsDataContext();
        var IndividualsListBoxRaw =
            (
            from x in LinqCtx.ViewIndividualsNames //not correct need individual view!
            orderby x.FullName
            select x
            );
        List<SelectListItem> items = new SelectList
               (
                IndividualsListBoxRaw, "First_Hospital_Case_Nbr", "FullName"
               ).ToList();
        items.Insert(0, (new SelectListItem { Text = "All Individuals", Value = "0.0", Selected = true }));
        Object = new SelectList
           (
           items,"Value","Text"
           );
        return Object;
       }
    
    
    4.
    
    private SelectList AddFirstItem(SelectList list)
      {
       List<SelectListItem> _list = list.ToList();
       _list.Insert(0, new SelectListItem() { Value = "-1", Text = "This Is First Item" });
       return new SelectList((IEnumerable<SelectListItem>)_list, "Value", "Text");
      }
    
    这应该做你需要的,只是发送您的选择列表 CodeGo.net,它会返回一个选择列表中的一个项目索引0 您可以将文字,值或项目,甚至索引你需要插入
    5. 使用C#扩展方法
    
    static public class SelectListHelper
    {
     static public SelectList Add(this SelectList list, string text, string value = "", ListPosition listPosition = ListPosition.First)
     {
      if (string.IsNullOrEmpty(value))
      {
       value = text;
      }
      var listItems = list.ToList();
      var lp = (int)listPosition;
      switch (lp)
      {
       case -1:
        lp = list.Count();
        break;
       case -2:
        lp = list.Count() / 2;
        break;
       case -3:
        var random = new Random();
        lp = random.Next(0, list.Count());
        break;
      }
      listItems.Insert(lp, new SelectListItem { Value = value, Text = text });
      list = new SelectList(listItems, "Value", "Text");
      return list;
     }
     public enum ListPosition
     {
      First = 0,
      Last = -1,
      Middle = -2,
      Random = -3
     }
    }
    
    用法(由为例):
    
    var model = new VmRoutePicker
     {
      Routes =
      new SelectList(_dataSource.Routes.Select(r => r.RouteID).Distinct())
      };          
     model.Routes = model.Routes.Add("All", "All", SelectListHelper.ListPosition.Random);
    //or
     model.Routes = model.Routes.Add("All");
    
    
    6. 听起来可能不是很优雅,但这样做:
    
     var items = repository.func.ToList();
     items.Insert(0, new funcItem { ID = 0, TextValue = "[None]" });
     ViewBag.MyData = new SelectList(items);
    
    
    7. 这是可能的。
    
    //Create the select list item you want to add
    SelectListItem selListItem = new SelectListItem() { Value = "null", Text = "Select One" };
    //Create a list of select list items - this will be returned as your select list
    List<SelectListItem> newList = new List<SelectListItem>();
    //Add select list item to list of selectlistitems
    newList.Add(selListItem);
    //Return the list of selectlistitems as a selectlist
    return new SelectList(newList, "Value", "Text", null);
    
    
    8. 我不知道如果任何人有一个更好的选择...
    
    <% if (Model.VariableName == "" || Model.VariableName== null) { %>
     <%= html.DropDpwnList("ListName", ((SelectList) ViewData["viewName"], "", 
      new{stlye=" "})%>
    <% } else{ %>
    <%= html.DropDpwnList("ListName", ((SelectList) ViewData["viewName"], 
      Model.VariableName, new{stlye=" "})%>
    <% }>
  • 相关阅读:
    迭代器Iterator与ConcurrentModificationException详解
    多线程 ForkJoinPool
    (转)面向对象——UML类图设计
    网络基础知识-网络协议
    Python 浅析线程(threading模块)和进程(process)
    一篇文章详解python的字符编码问题
    浅析文本挖掘(jieba模块的应用)
    浅析网站的基本构成和主要类型
    python 常用算法学习(1)
    浅谈使用git进行版本控制
  • 原文地址:https://www.cnblogs.com/GallopingSnail/p/5911941.html
Copyright © 2020-2023  润新知