ASP.NET Core提交模型问题:PanelComposition空值与下拉框异常
Got it, let's sort out why all your dropdowns are showing the same selected option. The core issue is how you're binding the <select> elements to your PanelComposition collection—you're not leveraging ASP.NET Core's tag helpers correctly for indexed model binding, which breaks the automatic selected state handling.
What Went Wrong
In your modified code, you manually set the name and value attributes on the <select> tag, but:
- The
nameattribute uses@Model.PanelComposition[i].JuryName, which doesn't match the property you need to bind to (FK_Jury) and lacks the proper indexed format for collection binding. - Manually setting
valuedoesn't work with theasp-itemstag helper—this helper handles selected state automatically when paired withasp-for.
The Fix
You need to use asp-for to bind directly to the FK_Jury property of each indexed PanelComposition item. This will:
- Generate the correct indexed
nameattribute (likePanelComposition[0].FK_Jury) that ASP.NET Core expects for collection model binding. - Automatically select the matching option from
JuryMembersbased on theFK_Juryvalue.
Here's the corrected code for your partial view, focusing on the dropdown section:
@model Models.Database.Models.PanelModel <div class="border-right"> <form asp-action="SavePanel" method="post"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <input type="hidden" asp-for="@Model.PanelName" /> <input type="hidden" asp-for="@Model.WedstrijdSessie" /> <div class="form-group"> <div class="row"> <div class="col-md-6 text-center d-flex align-items-center justify-content-center"> <input type="submit" value="Aanpassen" class="btn btn-outline-info" /> </div> <div class="col-md-6 text-center d-flex align-items-center justify-content-center"> <input type="button" value="Terug naar het overzicht" class="btn btn-outline-info" onclick="location.href = '@Url.Action("Index", "JuryOpsteller", new { id = Model.WedstrijdSessie })'" /> </div> </div> </div> <h3>@Model.PanelName</h3> <div class="container-fluid mb-3"> <div class="container bg-light"> @for (var i = 0; i < Model.PanelComposition.Count; i++) { <hr style="border-style: dotted;" /> <div class="row formgroup align-items-center"> <div class="col-4"> <label class="control-label"> <p class="font-weight-bold">@Model.PanelComposition[i].Functie:</p> </label> </div> <div class="col-8"> <!-- Corrected select binding --> <select asp-for="@Model.PanelComposition[i].FK_Jury" asp-items="@Model.JuryMembers" class="form-control text-center"> </select> <!-- Update validation to match the bound property if needed --> <span asp-validation-for="@Model.PanelComposition[i].FK_Jury" class="text-danger"></span> </div> </div> } </div> </div> </form> </div>
Key Notes
- Indexed Binding: Using
for(instead offoreach) is correct here because it gives us an index to reference, which is required for ASP.NET Core to bind collection items back to the model. - Tag Helper Magic: The
asp-for="@Model.PanelComposition[i].FK_Jury"does three critical things:- Generates the proper
nameattribute (e.g.,PanelComposition[0].FK_Jury) for model binding. - Reads the current
FK_Juryvalue of the item. - Matches that value to the
Valueproperty of options inJuryMembersto set the selected state.
- Generates the proper
- SelectList Setup: Ensure your
JuryMembersproperty is aSelectListwhere each option'sValueis theFK_Juryvalue, andTextis the display name (likeJuryName). For example, in your controller:model.JuryMembers = new SelectList(juryList, "FK_Jury", "JuryName");
Bonus Cleanup
I also removed the redundant id attributes from your hidden inputs—asp-for will generate these automatically, and you don't need them unless you're targeting the elements with JavaScript.
This should fix the uniform dropdown selection issue while keeping your model binding working correctly.
内容的提问来源于stack exchange,提问作者Fysicus

