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

如何遍历IEnumerable,在Count≠0时将索引1的值显示到文本框?

需求

遍历IEnumerable集合,当集合元素数量不为0时,将索引为1的元素对应值显示到文本框中。


右侧表格相关代码

后端逻辑代码

public PartialViewResult OnGetDisplayOwnerInfoTable(int value) =>
   Partial(
      "_DisplayDMVPartial", 
      _context
      .ExemptionApplicationDmvinformations
      .Where(x => x.ExemptionApplicationOwnerId == value)
      .ToList()
   );

前端视图代码

<tbody>
  <tr>
    <td style="border: 1px solid black; font-weight: bold; text-align: center;">
      @Html.DisplayNameFor(m => m.DmvDob)
    </td>
    <td style="border: 1px solid black; font-weight: bold; text-align: center;">
      @Html.DisplayNameFor(m => m.DriverLicense)
    </td>
  </tr>
@foreach (Models.ExemptionApplicationDmvinformation item in Model) {
  <tr>
    <!-- 
      <td style="border: 1px solid black; text-align: center;">
        item.DmvDob.Value.ToString("MMddyyyy")
      </td>
    -->
    <td style="border: 1px solid black; text-align: center;">
      @item.DmvDob
    </td>
    <td style="border: 1px solid black; text-align: center;">
      @item.DriverLicense
    </td>
  </tr>
}
</tbody>

左侧文本框关联代码

后端接口代码

public JsonResult OnGetDisplayOwnerInfo(int value) {
   ExemptionApplicationDmvinformation data =
      _context
      .ExemptionApplicationDmvinformations
      .Where(x => x.ExemptionApplicationOwnerId == value)
      .FirstOrDefault();

   return new JsonResult(new {
      DateOfBirth = data.DmvDob.Value.ToString("MMddyyyy"),
      DriversLicenseNumber = data.DriverLicense
   });
}

模型定义代码

[Display(Name = "出生日期")] public DateTime? DmvDob { get; set; }
[Display(Name = "驾照编号")] public string? DriverLicense { get; set; }

相关截图

界面截图1
界面截图2
界面截图3
界面截图4
界面截图5
界面截图6
界面截图7
界面截图8


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:15:22