简体   繁体   English

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

[英]Order dropdown list in ASP.Net mvc 5 application

I have a dropdown and I want it to appear in a particular order where dropdown option header should come before footer . 我有一个下拉菜单,我希望它以特定的顺序显示,其中下拉选项header应位于footer之前。 but I am not able to do so. 但我无法这样做。

Helper 帮手

public static readonly string HEADER_ID = "-1000";
public static readonly string FOOTER_ID = "-1001";

CSHTML 网页HTML

<select id="simTextEditorSelection" onchange="ShowTextEditorBasedOnSelection();"  style="float:right;">
    @foreach (PageInfoMV anItemForEditor in Model.ItemContents)
    {
        <option value="@anItemForEditor.ItemId">@anItemForEditor.ItemDisplayText</option>
    }

UI 用户界面

在此处输入图片说明

PS: I don't want to change the enum values of Header and footer. PS:我不想更改页眉和页脚的枚举值。 Please guide me. 请指导我。

My Attempt: 我的尝试:

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

But it created some other issues. 但这带来了其他一些问题。 So, I want to avoid it. 因此,我想避免这种情况。

Assume you have this class: 假设您有此类:

public class MyClass
{
    public int Id { get; set; }
    public int Name { get; set; }
}

In the Action you can create a SelectList from any list as followed: 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();
}

In the View : View

@{
    var optionList = ViewBag.Selectlist as SelectList;
}

@Html.DropDownListFor(model => model.someProperty, optionlist, "- Select an option -")

Change your cshtml to this and you should have your wish 将您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM