如何更新 .NET Core 中 List<T> 对象中的元素

问题描述 投票:0回答:1

我们如何更新列表类型而不是添加行而不是更新。在下面的代码中,我实现的是,首先我有一个视图,以可编辑格式在表中显示对象列表。这一切都很好,但是我面临的挑战是当我对列表进行更改或更新列表然后单击“提交”按钮时。请注意,这里我没有添加新对象,我只是更新现有对象。所以我可能会更新第 1 行、第 2 行或更多。 在操作时执行方法添加新行而不是更新现有行。这是我的代码:

查看

    <table class="table align-middle mb-0 bg-white table-hover table-bordered">
    <thead>
        <tr>
            <th>Defect ID</th>
            <th>
                <label>JobId</label>
            </th>
            <th>
                <label>Created date</label>
            </th>
            <th>
                <label>Created By</label>
            </th>
            
             
             <th>
                ProjectName
            </th>
             <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <form asp-action="Defectlist" method="post">
            @for (var i=0; i < Model.Count(); i++) {
                <tr>
                <td>@Html.DisplayFor(a => a[i].DefectId)</td>
            <td>
                @Html.DisplayFor(a=>a[i].JobId)
            </td>
            <td>
                @Html.DisplayFor(a => a[i].DefectCreatedDate)
            </td>
            <td>
                @Html.DisplayFor(a => a[i].DefectCreatedBy)
            </td>
            
            
            <td>
                    <select asp-for="@Model[i].ProjectId" asp-items="@ViewBag.List"></select>
            </td>
            
             @* <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.DefectId }) |
                @Html.ActionLink("Details", "Details", new { id=item.DefectId }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td> *@
        </tr>
            }
        <div class="form-group">
            <input type="submit" value="Save" class="btn btn-primary" />
        </div>
        </form>
    </tbody>
</table>

这是控制器操作

[HttpPost]
public IActionResult Defectlist(List<Defect> model)
{
    
    if (ModelState.IsValid)
    {
        foreach(var i in model)
        {
            db.Defects.Add(i);        
        }
        db.SaveChanges();
        TempData["SM"] = "Task updated";

        
    }
    return RedirectToAction("Defectlist");

}
c# asp.net-core asp.net-core-mvc
1个回答
0
投票

如果你只是想知道如何更新这个Defectlist方法中的行,我建议你可以参考这个官方文档

由于我不知道您是否要更新此缺陷模型的某些列,我建议您可以尝试以下代码:

通过这种方式,它会首先查询结果并只更新某些列而不是所有列。

如下:

      if (ModelState.IsValid)
      {
          foreach (var i in model)
          {
              var defectToUpdate = await _applicationDbContext.Defects.FirstOrDefaultAsync(s => s.DefectId == i.DefectId);

              if (await TryUpdateModelAsync<Defect>(
       i,
       "",
       s => s.JobId, s => s.DefectCreatedDate, s => s.DefectCreatedBy))
              {
                  try
                  {
                      await _applicationDbContext.SaveChangesAsync();
                     // return RedirectToAction(nameof(Index));
                  }
                  catch (DbUpdateException /* ex */)
                  {
                      //Log the error (uncomment ex variable name and write a log.)
                      ModelState.AddModelError("", "Unable to save changes. " +
                          "Try again, and if the problem persists, " +
                          "see your system administrator.");
                  }
              }
          }
          
          TempData["SM"] = "Task updated";


      }
© www.soinside.com 2019 - 2024. All rights reserved.