ASP.Net mvc 5应用程序中的订单下拉列表

时间:2016-12-02 22:20:39

标签: javascript jquery asp.net asp.net-mvc-5

我有一个下拉列表,我希望它以特定顺序显示,其中下拉选项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

enter image description here

P.S:我不想改变页眉和页脚的枚举值。请指导我。

我的尝试:

@foreach (PageInfoMV anItemForEditor in Model.ItemContents.OrderByDescending(x=>x.Id))

但它创造了一些其他问题。所以,我想避免它。

1 个答案:

答案 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 -")