У меня есть представление, которое отображает несколько строк из базы данных.Я использую 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>