我有一个视图,显示数据库中的多行。我正在使用For Loop
来显示它们并作为IList
传递。我只想使用已更改的表单字段来更新数据库。
在调试时,我没有得到数据库/表单字段中的每一行。只有7行。我在下面遇到异常。我在做什么错了?
System.InvalidOperationException:'附加类型为'TeamWeb.Models.Current_Antenna'的实体'失败,因为相同类型的另一个实体已经具有相同的主键值。如果图形中的任何实体具有相互冲突的键值,则使用“附加”方法或将实体的状态设置为“不变”或“修改”时,可能会发生这种情况。这可能是因为某些实体是新实体,尚未收到数据库生成的键值。在这种情况下,请使用“添加”方法或“已添加”实体状态来跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改”。
型号
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: <br />
Qty:
</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"> </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>
答案 0 :(得分:0)
request_2
答案 1 :(得分:0)
在您的课程Current_Antenna
中,rfds_processing_id
是一个Guid,这意味着它可能是主键,并且基于ActionResult Current(Guid? id, string sector)
,传入了相同的Guid。
您实际上可以从数据库中获取已更改的项目并更新这些项目
[HttpPost]
[ValidateAntiForgeryToken]
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)
{
Current_Antenna db_item = null
foreach (Current_Antenna item in current)
{
db_item = db.FirstOrDefault(m => m.rfds_processing_id == item.rfds_processing_id);
if(db_item != null)
db.Entry(db.item).CurrentValues.SetValues(item);
}
db.SaveChanges();
}
return View(current);
}