You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core提交模型问题:PanelComposition空值与下拉框异常

Fixing Uniform Dropdown Selection in ASP.NET Core Partial View

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:

  1. The name attribute 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.
  2. Manually setting value doesn't work with the asp-items tag helper—this helper handles selected state automatically when paired with asp-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 name attribute (like PanelComposition[0].FK_Jury) that ASP.NET Core expects for collection model binding.
  • Automatically select the matching option from JuryMembers based on the FK_Jury value.

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

  1. Indexed Binding: Using for (instead of foreach) 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.
  2. Tag Helper Magic: The asp-for="@Model.PanelComposition[i].FK_Jury" does three critical things:
    • Generates the proper name attribute (e.g., PanelComposition[0].FK_Jury) for model binding.
    • Reads the current FK_Jury value of the item.
    • Matches that value to the Value property of options in JuryMembers to set the selected state.
  3. SelectList Setup: Ensure your JuryMembers property is a SelectList where each option's Value is the FK_Jury value, and Text is the display name (like JuryName). 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:55:44