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

点击Update按钮无法填充DropDownListFor控件问题求助

问题解决:DropDownListFor点击更新按钮无法填充选中值

问题根源

你的DropDownListFor绑定的是Repairs.musteriID,生成的<option>标签value属性对应musteri_id(客户ID),但你在更新按钮的data-customer里存的是客户名称musteriAdSoyad,用val()设置名称时,找不到对应value的选项,自然无法选中。

修复步骤

  1. 修改更新按钮的data属性
    把按钮里的data-customer换成客户ID,因为DropDownList的选项值是ID而非名称:
<button type="button" class="btn btn-success btn-update" data-id="@item.bakim_id" data-customer-id="@item.musteri_id" data-product="@item.urunAd" data-date="@item.alinanTarih" data-month="@item.ay">
    <i class="fas fa-edit"></i>
</button>
  1. 修正JavaScript代码
    用客户ID设置DropDownList的选中值,同时修正拼写错误(repiar应为repair):
$(document).on('click', '.btn-update', function () {
    openModal(true);
    $("#repair_id").val($(this).data("id"));
    // 用客户ID设置下拉框选中
    $("#repairCustomerInput").val($(this).data("customer-id"));
    // 修正拼写错误:repiar → repair
    $("#repairProductInput").val($(this).data("product"));
    $("#repairDateInput").val($(this).data("date"));
    $("#repairMonthInput").val($(this).data("month"));
});

额外说明

如果确实需要通过客户名称匹配(不推荐,名称可能重复),可以用以下方式查找对应选项并设置选中:

const customerName = $(this).data("customer");
$("#repairCustomerInput option:contains(" + customerName + ")").prop("selected", true);

但优先推荐用ID匹配,ID是唯一标识,不会出现匹配错误。

内容的提问来源于stack exchange,提问作者sdmngz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 01:37:36