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:
Repository: test data
You can view the above source code from here: 191748-source-code.txt
Then, the result like this:
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.