提交表单时更新数据库中的多行 [英] Update multiple rows in the database on form submission

查看:54
本文介绍了提交表单时更新数据库中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图,显示数据库中的多行。我正在使用 For循环来显示它们,并作为 IList 传递。我只想使用已更改的表单字段来更新数据库。

I have a View that displays multiple rows from the database. I am using a For Loop to display them and passing as IList. I want to update the database with only the form fields that have changed.

在调试时,我不会在数据库/表单字段中得到每一行。只有7行。我在下面遇到异常。我在做什么错?

When debugging I am not getting every row in the database/form fields. There are only 7 rows. I am getting the exception below. What am I doing wrong?


System.InvalidOperationException:'附加类型为'TeamWeb.Models.Current_Antenna'的实体失败,因为另一个实体相同类型已经具有相同的主键值。如果图形中的任何实体具有相互冲突的键值,则使用附加方法或将实体的状态设置为不变或已修改时,可能会发生这种情况。这可能是因为某些实体是新实体,尚未收到数据库生成的键值。在这种情况下,请使用添加方法或已添加实体状态来跟踪图形,然后根据需要将非新实体的状态设置为未更改或已修改。

System.InvalidOperationException: 'Attaching an entity of type 'TeamWeb.Models.Current_Antenna' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.'

模型

public partial class Current_Antenna
{
    public System.Guid rfds_processing_id { get; set; }
    public int rfds_id { get; set; }
    public string type { get; set; }
    public string sector { get; set; }
    public Nullable<int> position { get; set; }
    public Nullable<int> qty { get; set; }
    public string model { get; set; }
    public int id { get; set; }
    public Nullable<System.DateTime> team_last_updated { get; set; }
    public string team_updated_by { get; set; }
}

控制器

public ActionResult Current(Guid? id, string sector)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    IList<Current_Antenna> current = db.Current_Antenna.ToList();
    current = current.Where(x => x.rfds_processing_id == id)
                    .OrderBy(x => x.sector == "A" ? 1 :
                                    x.sector == "B" ? 2 :
                                    x.sector == "C" ? 3 :
                                    x.sector == "D" ? 4 :
                                    x.sector == "E" ? 5 :
                                    x.sector == "F" ? 6 : 6)
                    .ThenBy(x => x.type == "ANTENNA MAKE - MODEL" ? 1 :
                          x.type == "Antenna RET Motor (QTY/MODEL)" ? 2 :
                          x.type == "SURGE ARRESTOR (QTY/MODEL)" ? 3 :
                          x.type == "DIPLEXER (QTY/MODEL)" ? 4 :
                          x.type == "DUPLEXER (QTY/MODEL)" ? 5 :
                          x.type == "Antenna RET CONTROL UNIT (QTY/MODEL)" ? 6 :
                          x.type == "TMA/LNA (QTY/MODEL)" ? 7 :
                          x.type == "CURRENT INJECTORS FOR TMA (QTY/MODEL)" ? 8 :
                          x.type == "PDU FOR TMAS (QTY/MODEL)" ? 9 :
                          x.type == "FILTER (QTY/MODEL)" ? 10 :
                          x.type == "SQUID (QTY/MODEL)" ? 11 :
                          x.type == "RRH - 700 band (QTY/MODEL)" ? 12 :
                          x.type == "RRH - 850 band (QTY/MODEL)" ? 13 :
                          x.type == "RRH - 1900 band (QTY/MODEL)" ? 14 :
                          x.type == "RRH - AWS band (QTY/MODEL)" ? 15 :
                          x.type == "RRH - WCS band (QTY/MODEL)" ? 16 :
                          x.type == "Additional RRH #1 - any band (QTY/MODEL)" ? 17 :
                          x.type == "Additional RRH #2 - any band (QTY/MODEL)" ? 18 :
                          x.type == "Additional Component 1 (QTY/MODEL)" ? 19 :
                          x.type == "Additional Component 2 (QTY/MODEL)" ? 20 :
                          x.type == "Additional Component 3 (QTY/MODEL)" ? 21 :
                          x.type == "DC TRUNK (QTY/MODEL)" ? 22 :
                          x.type == "DC BLOCK (QTY/MODEL)" ? 23 : 23)
                    .ThenBy(x => x.position == 1 ? 1 :
                          x.position == 2 ? 2 :
                          x.position == 3 ? 3 :
                          x.position == 4 ? 4 :
                          x.position == 5 ? 5 :
                          x.position == 6 ? 6 :
                          x.position == 7 ? 7 : 7)
                    .Select(x => new Current_Antenna
                    {
                        id = x.id,
                        rfds_id = x.rfds_id,
                        rfds_processing_id = x.rfds_processing_id,
                        sector = x.sector,
                        position = x.position,
                        type = x.type,
                        model = x.model,
                        qty = x.qty,
                        team_last_updated = x.team_last_updated,
                        team_updated_by = x.team_updated_by
                    }).ToList();

        return View(current);

    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    //public ActionResult Current(IList<Current_Antenna> current)
    public ActionResult Current([Bind(Include = "id,rfds_id,rfds_processing_id,sector,type,position,qty,model,team_last_updated,team_updated_by")] IList<Current_Antenna> current)
    {
        if (ModelState.IsValid)
        {
            foreach (Current_Antenna item in current)
            {
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();
            }                             
        }
        return View(current);
    }

查看

@model IList<TeamWeb.Models.Current_Antenna>

@using System.Data

@{
ViewBag.Title = "TEAM: Current Antennas";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container2">
    <h3 class="h3-text">CURRENT ANTENNAS</h3>


    @{
        dynamic idGuid = Url.RequestContext.RouteData.Values["id"];
        Guid id = new Guid(idGuid);

        string urlSector = Request.QueryString["sector"];
        if (Model.Any())
        {
            if (urlSector == " ")
            {
                foreach (var item in Model.Select(x => x.sector).First())
                {
                    urlSector = item.ToString();
                }
            }
        }

    }

    <div class="nav">
        @Html.ActionLink("BACK TO REPORT", "Review", new { id = id })
    </div>
    <hr />
    <br />
    @using (Html.BeginForm("Current", "RFDS", FormMethod.Post, new { id = id, enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()

        <table align="center" style="width:1400px;border-spacing:0;">
            <tr style="height:25px;">
                <td colspan="8" style="font-weight:700;text-align:center;">
                    <table align="center" style="width:1200px;border-spacing:0;">
                        <tr class="rev-tr">
                            @if (!Model.Any())
                            {
                                <td style="font-weight:700;text-align:center;color:#e30000;">
                                    THIS IS A NEW SITE. IT DOES NOT HAVE AN EXISTING ANTENNA CONFIGURATION.
                                </td>
                            }
                            @foreach (var item in Model.OrderBy(x => x.sector).Select(x => x.sector).Distinct())
                            {
                                if (urlSector == item)
                                {
                                    <td style="font-weight:700;text-align:center;color:#e30000;">
                                        SECTOR @item
                                    </td>
                                }
                                else
                                {
                                    <td style="font-weight:700;text-align:center;text-decoration:underline;">
                                        <a href="@Url.Action("Current", new { id = id, sector = @item })" class="a-edit">SECTOR @item</a>
                                    </td>
                                }
                            }

                        </tr>
                    </table>
                </td>
            </tr>
            <tr style="height:25px;">
                <th style="font-size:13px;font-weight:700;border-bottom:.5px solid;width:12.5%;">
                </th>
                <th style="font-size:13px;font-weight:700;border-bottom:.5px solid;width:12.5%;">
                    Position 1
                </th>
                <th style="font-size:13px;font-weight:700;border-bottom:.5px solid;width:12.5%;">
                    Position 2
                </th>
                <th style="font-size:13px;font-weight:700;border-bottom:.5px solid;width:12.5%;">
                    Position 3
                </th>
                <th style="font-size:13px;font-weight:700;border-bottom:.5px solid;width:12.5%;">
                    Position 4
                </th>
                <th style="font-size:13px;font-weight:700;border-bottom:.5px solid;width:12.5%;">
                    Position 5
                </th>
                <th style="font-size:13px;font-weight:700;border-bottom:.5px solid;width:12.5%;">
                    Position 6
                </th>
                <th style="font-size:13px;font-weight:700;border-bottom:.5px solid;width:12.5%;">
                    Position 7
                </th>
            </tr>

            @for (int i = 0; i < Model.Count; i++)
            {

                if (Model[i].sector == urlSector)
                {

                    //if (i % 6 == 0)
                    //{

                        <tr style="height:55px;">
                            <td style="text-align:right;font-size:13px;font-weight:600;border-bottom:.5px solid;white-space:nowrap;">
                                @Model[i].type:&nbsp;<br />
                                Qty:&nbsp;
                            </td>

                            <td style="text-align:center;border-bottom:.5px solid;border-left:.5px solid #808080;">

                                @if (Model[i].qty > 0 && Model[i].model == "")
                                {
                                    Model[i].qty = 0;
                                }

                                @Html.HiddenFor(x => Model[i].id)
                                @Html.HiddenFor(x => Model[i].rfds_id)
                                @Html.HiddenFor(x => Model[i].rfds_processing_id)
                                @Html.HiddenFor(x => Model[i].sector)
                                @Html.HiddenFor(x => Model[i].type)
                                @Html.HiddenFor(x => Model[i].position)
                                @Html.HiddenFor(x => Model[i].team_last_updated)
                                @Html.HiddenFor(x => Model[i].team_updated_by)

                                @Html.TextBoxFor(m => Model[i].model, new { style = "font-size:13px;width: 150px;text-align:center;" })<br />
                                @Html.TextBoxFor(m => Model[i].qty, new { style = "font-size:13px;width: 40px;text-align:center;" })

                                @{i++;}
                            </td>

                            <td style="text-align:center;border-bottom:.5px solid;border-left:.5px solid #808080;">

                                @if (Model[i].qty > 0 && Model[i].model == "")
                                {
                                    Model[i].qty = 0;
                                }

                                @Html.HiddenFor(x => Model[i].id)
                                @Html.HiddenFor(x => Model[i].rfds_id)
                                @Html.HiddenFor(x => Model[i].rfds_processing_id)
                                @Html.HiddenFor(x => Model[i].sector)
                                @Html.HiddenFor(x => Model[i].type)
                                @Html.HiddenFor(x => Model[i].position)
                                @Html.HiddenFor(x => Model[i].team_last_updated)
                                @Html.HiddenFor(x => Model[i].team_updated_by)

                                @Html.TextBoxFor(m => Model[i].model, new { style = "font-size:13px;width: 150px;text-align:center;" })<br />
                                @Html.TextBoxFor(m => Model[i].qty, new { style = "font-size:13px;width: 40px;text-align:center;" })

                                @{i++;}
                            </td>

                            <td style="text-align:center;border-bottom:.5px solid;border-left:.5px solid #808080;">

                                @if (Model[i].qty > 0 && Model[i].model == "")
                                {
                                    Model[i].qty = 0;
                                }

                                @Html.HiddenFor(x => Model[i].id)
                                @Html.HiddenFor(x => Model[i].rfds_id)
                                @Html.HiddenFor(x => Model[i].rfds_processing_id)
                                @Html.HiddenFor(x => Model[i].sector)
                                @Html.HiddenFor(x => Model[i].type)
                                @Html.HiddenFor(x => Model[i].position)
                                @Html.HiddenFor(x => Model[i].team_last_updated)
                                @Html.HiddenFor(x => Model[i].team_updated_by)

                                @Html.TextBoxFor(m => Model[i].model, new { style = "font-size:13px;width: 150px;text-align:center;" })<br />
                                @Html.TextBoxFor(m => Model[i].qty, new { style = "font-size:13px;width: 40px;text-align:center;" })

                                @{i++;}
                            </td>

                            <td style="text-align:center;border-bottom:.5px solid;border-left:.5px solid #808080;">

                                @if (Model[i].qty > 0 && Model[i].model == "")
                                {
                                    Model[i].qty = 0;
                                }

                                @Html.HiddenFor(x => Model[i].id)
                                @Html.HiddenFor(x => Model[i].rfds_id)
                                @Html.HiddenFor(x => Model[i].rfds_processing_id)
                                @Html.HiddenFor(x => Model[i].sector)
                                @Html.HiddenFor(x => Model[i].type)
                                @Html.HiddenFor(x => Model[i].position)
                                @Html.HiddenFor(x => Model[i].team_last_updated)
                                @Html.HiddenFor(x => Model[i].team_updated_by)

                                @Html.TextBoxFor(m => Model[i].model, new { style = "font-size:13px;width: 150px;text-align:center;" })<br />
                                @Html.TextBoxFor(m => Model[i].qty, new { style = "font-size:13px;width: 40px;text-align:center;" })

                                @{i++;}
                            </td>

                            <td style="text-align:center;border-bottom:.5px solid;border-left:.5px solid #808080;">

                                @if (Model[i].qty > 0 && Model[i].model == "")
                                {
                                    Model[i].qty = 0;
                                }

                                @Html.HiddenFor(x => Model[i].id)
                                @Html.HiddenFor(x => Model[i].rfds_id)
                                @Html.HiddenFor(x => Model[i].rfds_processing_id)
                                @Html.HiddenFor(x => Model[i].sector)
                                @Html.HiddenFor(x => Model[i].type)
                                @Html.HiddenFor(x => Model[i].position)
                                @Html.HiddenFor(x => Model[i].team_last_updated)
                                @Html.HiddenFor(x => Model[i].team_updated_by)

                                @Html.TextBoxFor(m => Model[i].model, new { style = "font-size:13px;width: 150px;text-align:center;" })<br />
                                @Html.TextBoxFor(m => Model[i].qty, new { style = "font-size:13px;width: 40px;text-align:center;" })

                                @{i++;}
                            </td>

                            <td style="text-align:center;border-bottom:.5px solid;border-left:.5px solid #808080;">

                                @if (Model[i].qty > 0 && Model[i].model == "")
                                {
                                    Model[i].qty = 0;
                                }

                                @Html.HiddenFor(x => Model[i].id)
                                @Html.HiddenFor(x => Model[i].rfds_id)
                                @Html.HiddenFor(x => Model[i].rfds_processing_id)
                                @Html.HiddenFor(x => Model[i].sector)
                                @Html.HiddenFor(x => Model[i].type)
                                @Html.HiddenFor(x => Model[i].position)
                                @Html.HiddenFor(x => Model[i].team_last_updated)
                                @Html.HiddenFor(x => Model[i].team_updated_by)

                                @Html.TextBoxFor(m => Model[i].model, new { style = "font-size:13px;width: 150px;text-align:center;" })<br />
                                @Html.TextBoxFor(m => Model[i].qty, new { style = "font-size:13px;width: 40px;text-align:center;" })

                                @{i++;}
                            </td>

                            <td style="text-align:center;border-bottom:.5px solid;border-left:.5px solid #808080;">

                                @if (Model[i].qty > 0 && Model[i].model == "")
                                {
                                    Model[i].qty = 0;
                                }

                                @Html.HiddenFor(x => Model[i].id)
                                @Html.HiddenFor(x => Model[i].rfds_id)
                                @Html.HiddenFor(x => Model[i].rfds_processing_id)
                                @Html.HiddenFor(x => Model[i].sector)
                                @Html.HiddenFor(x => Model[i].type)
                                @Html.HiddenFor(x => Model[i].position)
                                @Html.HiddenFor(x => Model[i].team_last_updated)
                                @Html.HiddenFor(x => Model[i].team_updated_by)

                                @Html.HiddenFor(x => Model[i].id)
                                @Html.TextBoxFor(m => Model[i].model, new { style = "font-size:13px;width: 150px;text-align:center;" })<br />
                                @Html.TextBoxFor(m => Model[i].qty, new { style = "font-size:13px;width: 40px;text-align:center;" })


                            </td>

                        </tr>   
                    //}                             
                }
            }

            <tr>
                <td colspan="8">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="8" align="center">
                    @if (Model.Any())
                    {
                        <input type="submit" value="Save Changes" class="btn btn-default" />
                    }
                </td>
            </tr>
        </table>
        <br />
        <hr />
        <div class="nav">
            @Html.ActionLink("BACK TO REPORT", "Review", new { id = id })
        </div>
        <br />
        <br />
        <br />
        <br />
    }

</div>


推荐答案

 if (ModelState.IsValid)
        {
            foreach (Current_Antenna item in current)
            {
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();
            }

        }

这篇关于提交表单时更新数据库中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆