6dev6il6
6dev6il6

Reputation: 857

How to sort @Html.DropDownListFor with a specific item last on the list

I have a list with Options:

ATI

FICA

OTHER

FATCA

SOFP

I would like to modify the order so that "Other" is listed last, is this possible?

My code looks like this:

@Html.DropDownListFor(x => x.DocumentTypeId, Model.DocumentTypes, new { style = "width:174px", @title = "Document Type" })

I have tried OrderBy but this only arranges alphabetically.

EDIT:

There are more document types than the few I listed and they are all called from a DB table depending on prior conditions. Nothing is hard coded

Upvotes: 0

Views: 3492

Answers (4)

Grizzly
Grizzly

Reputation: 5943

In my projects that require tasks such as this I create a separate c# file and create a method like so:

public static List<SelectListItem> lstOptionsInOrder()
{
    List<SelectListItem> lstOptions = new List<SelectListItem>();

    List<string> lstOptionsOrder = new List<string>() {"ATI", "FICA", "FATCA", "SOFP", "OTHER"};

    var optionsInOrder = db.DocumentTableName.OrderBy(x => lstOptionsOrder.IndexOf(x.DocumentTypes));

    foreach(var option in optionsInOrder){
        SelectListItem item = new SelectListItem() {Text = option.DocumentTypes, Value = option.ID.ToString()};
        lstOptions.Add(item);
    }

return lstOptions;
}

Then in your Controller:

ViewBag.lstOrderOptions = /*NameOfC#File*/.lstOptionsInOrder();

Then in your View:

@Html.DropDownList("lstOrderOptions", null, "-- Select Option --", htmlAttributes: new { @class = "form-control"})

Upvotes: 1

Rion Williams
Rion Williams

Reputation: 76547

Ordering Your Elements

You could consider sorting it based on that particular element via an OrderBy() method call, which would place your "ORDER" element at the end of the list :

<!-- This will maintain your order, except place "ORDER" at the end of the list -->
@Html.DropDownListFor(x => x.DocumentTypeId, Model.DocumentTypes.OrderBy(d => d == "ORDER"), ... )

You can see an example of this in action here.

Other Considerations

Generally, you should avoid incorporating this type of logic within your Views directly. You would be better off handling this within your Model itself prior to passing it to the View (either by explicitly setting the order of the values that you want to use, or by performing an OrderBy() call similar to that mentioned above) :

// Order prior to passing to the View
Model.DocumentTypes = Model.DocumentTypes.OrderBy(d => d == "ORDER").ToList();
return View(Model);

Upvotes: 1

Andrei Dragotoniu
Andrei Dragotoniu

Reputation: 6335

the easiest solution would be create a sorted list on the server side with your 4 options and then simply add Other as the last item in your list. You then pass your data to your model already prepared and let the view render the html normally.

Upvotes: 1

Andre Albuquerque
Andre Albuquerque

Reputation: 1931

You would have to implement your own custom implementation of DropDownListFor.

You can start looking here (stackoverflow) for a few examples.

Upvotes: 1

Related Questions