C# 如何对 ASp.net MVC 下拉列表进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1698266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort an ASp.net MVC dropdownlist?
提问by chobo2
I have this code
我有这个代码
List<SelectListItem> list = new List<SelectListItem>()
{
new SelectListItem() { Text = "bob", Value = "bob"},
new SelectListItem() { Text = "apple", Value = "apple"},
new SelectListItem() { Text = "grapes", Value = "grapes"},
};
This will be used for binding with the asp.net mvc html helper. However I want to sort it before I bind it. How could i do this?
这将用于与 asp.net mvc html helper 绑定。但是我想在绑定之前对其进行排序。我怎么能这样做?
采纳答案by LukLed
Here you go:
干得好:
List<SelectListItem> list = new List<SelectListItem>()
{
new SelectListItem() { Text = "apple", Value = "apple"},
new SelectListItem() { Text = "bob", Value = "bob"},
new SelectListItem() { Text = "grapes", Value = "grapes"},
};
Sorted:)
排序:)
Sorry, couldn't stop myself:)
对不起,无法阻止自己:)
EDIT
编辑
It looks as if you needed:
看起来你需要:
var fruits = new List<string> {"apple", "bob", "grapes"};
fruits.Sort();
var fruitsSelectList = new SelectList(fruits);
and then in view
然后在视图中
Html.DropDownList("Fruit",fruitsSelectList);
回答by griegs
If you can use LINQ then:
如果您可以使用 LINQ,则:
list.OrderBy(x => x.Value)
or
或者
list.OrderByDescending(x =>x.Value)
should do it.
应该这样做。
edit
编辑
That should read;
那应该读;
list = list.OrderBy(x => x.Value);
回答by Josh
var sorted = (from li in list
orderby li.Text
select li).ToList();
Voila!!
瞧!!
回答by Hannoun Yassir
you can also sort it in the client side using javascript (jquery)
您也可以使用 javascript (jquery) 在客户端对其进行排序
BTW if you know the elements of the list just sort them yourself :
顺便说一句,如果您知道列表中的元素,只需自己对它们进行排序:
List<SelectListItem> list = new List<SelectListItem> {
new SelectListItem { Text = "apple", Value = "apple"},
new SelectListItem { Text = "bob", Value = "bob"},
new SelectListItem { Text = "grapes", Value = "grapes"}
};
回答by Chuckie
list.Sort
列表.排序
List<SelectListItem> list = new List<SelectListItem>()
{ new SelectListItem() { Text = "bob", Value = "bob"},
new SelectListItem() { Text = "apple", Value = "apple"},
new SelectListItem() { Text = "grapes", Value = "grapes"}, };
{ new SelectListItem() { Text = "bob", Value = "bob"},
new SelectListItem() { Text = "apple", Value = "apple"},
new SelectListItem() { Text = "grapes", Value = "葡萄"}, };
list.sort;
列表排序;
回答by Htun Thein Win
-------Store Procedure-----(SQL)
-------存储过程-----(SQL)
USE [Your Database]
GO
CRATE PROC [dbo].[GetAllDataByID]
@ID int
AS
BEGIN
SELECT * FROM Your_Table
WHERE ID=@ID
ORDER BY Your_ColumnName
END
----------Default.aspx---------
----------默认.aspx---------
<asp:DropDownList ID="ddlYourTable" runat="server"></asp:DropDownList>
---------Default.aspx.cs-------
---------Default.aspx.cs-------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<YourTable> table= new List<YourTable>();
YourtableRepository tableRepo = new YourtableRepository();
int conuntryInfoID=1;
table= tableRepo.GetAllDataByID(ID);
ddlYourTable.DataSource = stateInfo;
ddlYourTable.DataTextField = "Your_ColumnName";
ddlYourTable.DataValueField = "ID";
ddlYourTable.DataBind();
}
}
-------LINQ Helper Class----
-------LINQ助手类----
public class TableRepository
{
string connstr;
public TableRepository()
{
connstr = Settings.Default.YourTableConnectionString.ToString();
}
public List<YourTable> GetAllDataByID(int ID)
{
List<YourTable> table= new List<YourTable>();
using (YourTableDBDataContext dc = new YourTableDBDataContext ())
{
table= dc.GetAllDataByID(CID).ToList();
}
return table;
}
}
回答by Red
Isn't the idea of MVC to separate function and display? What if you want to reuse the same list with different orderings?
MVC的思想不是功能和显示分离吗?如果您想以不同的顺序重复使用相同的列表怎么办?
I'd have thought this would be best as it only sorts if for the specified control.
我原以为这是最好的,因为它只对指定的控件进行排序。
Add a property to the Model you are using for the view:
向您用于视图的模型添加一个属性:
public SelectList Fruit { get; set; }
Populate that list in your constructor (I'm using Entity Framework):
在您的构造函数中填充该列表(我使用的是实体框架):
model.Fruit= new SelectList(db.tblFruit.Select(f => new { Id = f.ID, Name = f.Name }), "ID", "Name", "[Select Fruit]");
Then add your select list:
然后添加您的选择列表:
@Html.DropDownListFor(x => x.ID, new SelectList(Model.Fruit.OrderBy(y => y.Text), "Value", "Text"), "-- Select One --", new { @class = "form-control" })
回答by michellhornung
A very simple way to handle it in Controller:
在 Controller 中处理它的一种非常简单的方法:
ViewBag.change_week = new SelectList(db.weeks.OrderBy(x=> x.week_guid), "week_guid", "week_number");