0

I will try my best to explain what I am looking for.

Let's say for argument's sake that I have a table in my database that has the properties ID, Item, Category...

the table is populated as so:

================================
|ID|    |Item       | Category |
================================
|  1    |  Batman   |   DC     |
|  2    |  Superman |   DC     |
|  3    |  Aquaman  |   DC     |
|  4    |  Spiderman|   Marvel |
|  5    |  Ironman  |   Marvel |
|  6    |  Thor     |   Marvel |
================================

Now, I want to create a dropdownlist with this information... but seperated by the category..

So it would look like this:

  1. DC
    • -Batman
    • -Superman
    • -Aquaman
  2. Marvel
    • -Spiderman
    • -Ironman
    • -Thor

Now it doesn't need to be as formatted as shown above but just a simple nested list with the emphasis on the Category

How would I iterate through the table and each time the Category changes to print that Category text once then print the Item's that correspond with that Category underneath it?

7
  • 1
    stackoverflow.com/questions/607188/… Commented Apr 4, 2016 at 19:52
  • How about something like this: weblogs.asp.net/raduenuca/… Commented Apr 4, 2016 at 19:56
  • Are you using asp.net-mvc? And if so which version? Commented Apr 4, 2016 at 22:55
  • @StephenMuecke Yes i am, version 5.2.3.0 Commented Apr 5, 2016 at 12:01
  • Then you can just use one of the overloads of SelectList that add the dataGroupField (or set the Group property of SelectListItem) and use @HtmlDropDownListFor() in your view Commented Apr 5, 2016 at 12:06

2 Answers 2

2

In MVC-5.2, you can use one of the overloads of SelectList that accepts string dataGroupField to group your options. For example, in the controller

model.OptionList = new SelectList(db.MyTable, "ID", "Item", "Category", null);
return View(model);

and in the view

@Html.DropDownListFor(m => m.MyProperty, Model.OptionList)

Alternatively you can build an IEnumerable<SelectListItem> and set the Group property, for example

List<SelectListItem> optionList = new List<SelectListItem>
var groups = db.MyTable.GroupBy(x => x.Category);
foreach(var group in groups)
{
    var optionGroup = new SelectListGroup() {Name = group.Key};
    foreach (var item in group)
    {
        optionList.Add(new SelectListItem { Value = item.ID.ToString(), Text = item.Item, Group = optionGroup });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

If you wanted to output your results as you initially provided, you could accomplish this by using a GroupBy call and then simply iterating through those groups (and subsequently the items within each group):

@foreach(var group in Model.GroupBy(g => g.Category))
{
     <ol>
        <li><b>@group.Key</b></li>
        <ul>
        @foreach(var item in group)
        {
            <li>- @item</li>
        }
        </ul>
     </ol>
}

This assumes that you already have the objects from your database within your Controller and are simply passing the collection of them into your View. Additionally, this example is performed within the View, which isn't ideal (you preferably want to handle this logic within the actual model itself).

As far as creating a DropDownList goes, you could really do the same if you wanted to preface each entry with its appropriate category :

<select id='Hero' name='Hero'>
@foreach(var hero in Model)
{
     <option value='hero.ID'>@hero.Category - @hero.Item</option>
}
</select>

9 Comments

You're also presuming he is using MVC and I don't see anything in the question to indicate that. Not saying he isn't, just saying it isn't really indicated by the question. The question doesn't even indicate a web application.
I only assumed this from the model-view-controller tag that was attached to the question.
Ok, good catch, I didn't even look at the tags, just read the question.
@Kevin yes i am using MVC
why not use optgroup and nest the grouped options inside... <select> <optgroup label="DC"> <option> Superman</option> </optgroup> <optgroup label="Marvel"> </optgroup> </select>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.