首页 > 代码库 > 如何将项目添加到在ASP.net MVC的选择列表
如何将项目添加到在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 = http://www.mamicode.com/"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 = http://www.mamicode.com/"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 = http://www.mamicode.com/"-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 = http://www.mamicode.com/"", 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 = http://www.mamicode.com/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 = http://www.mamicode.com/"[None]" }); ViewBag.MyData = new SelectList(items);7. 这是可能的。//Create the select list item you want to addSelectListItem selListItem = new SelectListItem() { Value = http://www.mamicode.com/"null", Text = "Select One" };//Create a list of select list items - this will be returned as your select listList<SelectListItem> newList = new List<SelectListItem>();//Add select list item to list of selectlistitemsnewList.Add(selListItem);//Return the list of selectlistitems as a selectlistreturn 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=" "})%><% }>
如何将项目添加到在ASP.net MVC的选择列表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。