gpt4 book ai didi

c# - 如何从 View 一次更新多行(ASP.NET - 核心)

转载 作者:行者123 更新时间:2023-12-05 01:32:50 24 4
gpt4 key购买 nike

我有一个 View ,其中显示了存储在我的数据库中的设备表,该数据库是使用 Code-First 和 Entity Framework Core 创建的。现在我的目标是同时更新多行的 EnableAlertsUpload bool 值。如下图所示。

enter image description here

现在,当我运行代码并按下更新按钮时,页面将只刷新而不更新 EnableAlertsUpload bool 值。对此的任何帮助将不胜感激,大多数解释只告诉 ho 通过 ID 更新单个值,而不是同时更新多行。

我的代码如下所示:

Controller

    public async Task<IActionResult> EditList()
{
var devices = db.Devices;

return View(devices);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditList(int id, [Bind("EnableAlertsUpload")] Devices[] devices)
{
if (ModelState.IsValid)
{
try
{
foreach (Devices dev in devices)
{
db.Update(dev);
}
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
return NotFound();
}
return RedirectToAction(nameof(IndexV2));
}

return View(devices);
}

编辑 ListView

@model IEnumerable<FrontlineAI.Models.Devices>

<h1>Devices</h1>

<form asp-action="EditList">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.objid)
</th>
<th>
@Html.DisplayNameFor(model => model.device)
</th>
<th>
@Html.DisplayNameFor(model => model.group)
</th>
<th>
@Html.DisplayNameFor(model => model.host)
</th>
<th>
@Html.DisplayNameFor(model => model.EnableAlertsUpload)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.objid)
</td>
<td>
@Html.DisplayFor(modelItem => item.device)
</td>
<td>
@Html.DisplayFor(modelItem => item.group)
</td>
<td>
@Html.DisplayFor(modelItem => item.host)
</td>
<td>
<td>@Html.CheckBoxFor(modelItem => item.EnableAlertsUpload)</td>
</td>
</tr>
}
</tbody>
</table>
<button input type="submit" value="Edit">Update</button>
</form>

设备型号

public partial class Devices
{
[Key()]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int objid { get; set; }
public string group { get; set; }
public string device { get; set; }
public string host { get; set; }
public bool EnableAlertsUpload { get; set; }
}

最佳答案

看来你没有成功绑定(bind)数据,你可以试试我下面的代码。

编辑 ListView

@model IEnumerable<Devices>

@{
ViewData["Title"] = "Edit";
}

<h1>Devices</h1>

<form asp-action="EditList">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.objid)
</th>
<th>
@Html.DisplayNameFor(model => model.device)
</th>
<th>
@Html.DisplayNameFor(model => model.group)
</th>
<th>
@Html.DisplayNameFor(model => model.host)
</th>
<th>
@Html.DisplayNameFor(model => model.EnableAlertsUpload)
</th>
<th></th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.HiddenFor(m => m.ToList()[i].objid)
@Html.DisplayFor(m => m.ToList()[i].objid)
</td>
<td>
@Html.HiddenFor(m => m.ToList()[i].device)
@Html.DisplayFor(m => m.ToList()[i].device)
</td>
<td>
@Html.HiddenFor(m => m.ToList()[i].group)
@Html.DisplayFor(m => m.ToList()[i].group)
</td>
<td>
@Html.HiddenFor(m => m.ToList()[i].host)
@Html.DisplayFor(m => m.ToList()[i].host)
</td>
<td>@Html.CheckBoxFor(m => m.ToList()[i].EnableAlertsUpload)</td>
</tr>
}
</tbody>
</table>
<button input type="submit" value="Edit">Update</button>
</form>

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditList(int id, List<Devices> devices)
{
if (ModelState.IsValid)
{
try
{
foreach (Devices dev in devices)
{
_context.Update(dev);
}
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
return NotFound();
}
}
return RedirectToAction(nameof(EditList));
}

结果 enter image description here

关于c# - 如何从 View 一次更新多行(ASP.NET - 核心),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65298688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com