[英]Order dropdown list in ASP.Net mvc 5 application
我有一个下拉菜单,我希望它以特定的顺序显示,其中下拉选项header
应位于footer
之前。 但我无法这样做。
帮手
public static readonly string HEADER_ID = "-1000";
public static readonly string FOOTER_ID = "-1001";
网页HTML
<select id="simTextEditorSelection" onchange="ShowTextEditorBasedOnSelection();" style="float:right;">
@foreach (PageInfoMV anItemForEditor in Model.ItemContents)
{
<option value="@anItemForEditor.ItemId">@anItemForEditor.ItemDisplayText</option>
}
用户界面
PS:我不想更改页眉和页脚的枚举值。 请指导我。
我的尝试:
@foreach (PageInfoMV anItemForEditor in Model.ItemContents.OrderByDescending(x=>x.Id))
但这带来了其他一些问题。 因此,我想避免这种情况。
假设您有此类:
public class MyClass
{
public int Id { get; set; }
public int Name { get; set; }
}
在Action
您可以从任何列表中创建一个SelectList
,如下所示:
public ActionResult Create()
{
var list = db.MyClass.ToList(); //or an other way to get the list of items
//you can add som more items to the list:
list.Add(new MyClass { Id=-1000, Name="Some Name" });
ViewBag.Selectlist = new SelectList(list.OrderByDescending(x=>x.Id), "Id", "Name");
return View();
}
在View
:
@{
var optionList = ViewBag.Selectlist as SelectList;
}
@Html.DropDownListFor(model => model.someProperty, optionlist, "- Select an option -")
将您的cshtml更改为此,您应该有一个愿望
<select id="simTextEditorSelection" onchange="ShowTextEditorBasedOnSelection();" style="float:right;">
@foreach (PageInfoMV anItemForEditor in Model.ItemContents.OrderByDescending())
{
<option value="@anItemForEditor.ItemId">@anItemForEditor.ItemDisplayText</option>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.