如何遍历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; }
相关截图








内容的提问来源于stack exchange,提问作者TheGame1083
相关产品推荐
相关产品推荐

