Adding a default SelectListItem

后端 未结 7 1200
一整个雨季
一整个雨季 2020-12-28 18:33
 public IEnumerable GetList(int? ID)
 {
      return from s in db.List
             orderby s.Descript
             select new SelectListItem
          


        
相关标签:
7条回答
  • 2020-12-28 18:44
    var list = from s in db.List
               orderby s.Descript
               select new SelectListItem
               {
                   Text = s.Descript,
                   Value = s.ID.ToString(),
                   Selected = (s.ID == ID)
               };
    
    list.Insert(0, new SelectListItem { Text = "Please Select...", Value = string.Empty });
    return list;
    
    0 讨论(0)
  • 2020-12-28 18:46

    After I reviewed many questions, and I don't find what i'm looking for.I don't need many lines of code to just have a simple drowpdown. so I want to share with you what I use and it easy and simple.. ( specially if you don't want to use and Entity Framework..

    using System.Web.Mvc;

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

    For Example:

    in Controller add :

    SelectList slTitle = new SelectList(Query.SelectAllTitle(), "TitleID", "TitleName");
    ViewBag.TitleSelectList = slTitle; 
    

    in View add :

    @Html.DropDownList("TitleSelectList", "--please select--")
    
    0 讨论(0)
  • 2020-12-28 18:48

    As you are using ASP.NET MVC, you can do this in the view by specifying a value for the optionLabel parameter of the DropDownField method of the HtmlHelper - e.g:

    htmlHelper.DropDownList("customerId", selectList, "Select One");
    

    Putting this type of code in your UI layer is probably more appropriate than having it in the data layer. One downside to doing this is that your select box will have an empty string value, not a "0" for the 'Select One' option, but that is not really a problem as you can treat this as a null value if your controller action method can accept a nullable int for the relevant parameter - e.g.

    public ActionResult DoSomething(int? customerId)
    {
      if(customerId != null)
      {
        // do something with the value
      }
    }
    
    0 讨论(0)
  • 2020-12-28 18:56
    return new[] { new SelectListItem { Text = ... } }.Concat(
           from s in db.List
           orderby s.Descript
           select new SelectListItem
           {
               Text = s.Descript,
               Value = s.ID.ToString(),
               Selected = (s.ID == ID)
           });
    
    0 讨论(0)
  • 2020-12-28 18:58

    firt put your default value in the list

    list.add(your default list item)

    and then do list.addrange(linq select query)

    cheers

    0 讨论(0)
  • 2020-12-28 19:01

    Here is what I did, I read my values from an XML file into an IList. I then inserted a new record into the IList at position 0. Then make a select list from the IList.

    IList< MY_DATA > mydata = (from tmp in myXML.Descendants("R").ToList()

                          select new MY_DATA
                          {
                             NR = tmp.Attribute("NR").Value,
                             NA = tmp.Attribute("NA").Value 
                          }).ToList<MY_DATA>();
    

    mydata.Insert(0, new My_DATA() { NR = "", NA = "--Click to Select--" });

    SelectList mylist = new SelectList(mydata, "NR", "NA");

    0 讨论(0)
提交回复
热议问题