如何从视图一次更新多行(ASP.NET-Core)

c#

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

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

我的代码如下所示:

控制器

    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);
    }

编辑列表视图

@model IEnumerable<FrontlineAI.Models.Devices>

<h1>Devices</h1>

<form asp-action="EditList">
    <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; }
}

回答

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

编辑列表视图

@model IEnumerable<Devices>

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

<h1>Devices</h1>

<form asp-action="EditList">
    <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>

控制器

[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));
    }

结果


以上是如何从视图一次更新多行(ASP.NET-Core)的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>