0

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

6dev6il6
  • 767
  • 2
  • 15
  • 34

4 Answers4

1

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

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

Community
  • 1
  • 1
Andre Albuquerque
  • 1,881
  • 1
  • 18
  • 15
1

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.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
  • Thanks, I found where the list was being populated, removed the item once the list was fully populated and then added it again which puts it last on the list. – 6dev6il6 Jul 14 '16 at 07:43
1

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);
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
1

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"})
Grizzly
  • 5,873
  • 8
  • 56
  • 109