Multiple Dropdown Lists, Same Items, Different Selected Item

Anna Tamaoka 136 Reputation points
2022-04-11T02:47:45.31+00:00

MVC, ASP.NET 6.0 Core, Entity Framework

For this example, I have a list of spices in the ViewBag. For each item in Model, generate a dropdown list with the list of spices. The default selected spice should be @item.SpiceId.

I don't understand where to add the logic that says "if the SpiceId is this item in the SelectList, select the item".

I've been searching, but I seem to only come across answers for cascading dropdown lists or multiple-select dropdown lists, not for the same dropdown list being populated with different selected items.

The View:

<table>  
        <tr>  
            <th>Spice</th>  
            <th>Price</th>  
            <th>Nice</th>  
        </tr>  
        @foreach (var item in Model)  
        {  
            <tr>  
                <td>  
                    <select asp-items="ViewBag.SpiceList"></select>  
                </td>  
                <td>@item.Price</td>  
                <td>@item.Nice</td>  
            </tr>  
        }  
    </table>  

The Controller:

var spicesList = _context.Spices.ToList();  
ViewBag.SpiceList = new SelectList(spicesList , "SpiceId", "SpiceName");  
return View(spicesList);  

The desired outcome: (each dropdown list populated with @item.SpiceId)
191663-spices.png

What it's doing instead: (because I don't know where to set the selectedItem)
191638-spices2.png

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
710 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,283 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,051 Reputation points Microsoft Vendor
    2022-04-11T07:51:37.243+00:00

    Hi @Anna Tamaoka ,

    For this example, I have a list of spices in the ViewBag. For each item in Model, generate a dropdown list with the list of spices. The default selected spice should be @item.SpiceId.

    I don't understand where to add the logic that says "if the SpiceId is this item in the SelectList, select the item".

    To set the default selected spices, you could add a "SelectSpiceId" property in the Spice class, then when bind the select tag, you could add an asp-for attribute specifies the SelectSpiceId property and set the default selected value. Like this:

        <select asp-for="@Model[i].SelectSpiceId"  class=" form-select" asp-items="ViewBag.SpiceList"></select>  
    

    You could refer the following sample:

    Model:

    191801-image.png

    Repository: test data

    191735-image.png

    Controller:

    191768-image.png

    View page:

    191822-image.png

    You can view the above source code from here: 191748-source-code.txt

    Then, the result like this:

    191736-1.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful