我有一个下拉列表,我希望它以特定顺序显示,其中下拉选项header
应该出现在footer
之前。但我无法这样做。
辅助
public static readonly string HEADER_ID = "-1000";
public static readonly string FOOTER_ID = "-1001";
CSHTML
<select id="simTextEditorSelection" onchange="ShowTextEditorBasedOnSelection();" style="float:right;">
@foreach (PageInfoMV anItemForEditor in Model.ItemContents)
{
<option value="@anItemForEditor.ItemId">@anItemForEditor.ItemDisplayText</option>
}
UI
P.S:我不想改变页眉和页脚的枚举值。请指导我。
我的尝试:
@foreach (PageInfoMV anItemForEditor in Model.ItemContents.OrderByDescending(x=>x.Id))
但它创造了一些其他问题。所以,我想避免它。
答案 0 :(得分:0)
假设您有这门课程:
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 -")