[英]Entities may have been modified or deleted since entities were loaded. (ASP.NET MVC)
I try to update one property in table from SelectedList 我尝试从SelectedList更新表中的一个属性
Here is my model 这是我的模特
public partial class Interwier
{
[Key]
public int Interwier_id { get; set; }
[Display(Name = "ФИО")]
public string FIO { get; set; }
[Display(Name = "Email")]
public string Email { get; set; }
[Display(Name = "Телефон")]
public string Telephone { get; set; }
[Display(Name = "День рождения")]
public System.DateTime Birthday { get; set; }
[Display(Name = "Город")]
public string City { get; set; }
[Display(Name = "Зарплата")]
public string Salary { get; set; }
[Display(Name = "Английский язык")]
public string English { get; set; }
public Nullable<int> VacancyId { get; set; }
public string Status { get; set; }
public virtual Vacancy Vacancy { get; set; }
}
I need to update Status
property. 我需要更新
Status
属性。
Here is Edit action in controller 这是控制器中的编辑动作
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Interwier interwierModel = db.InterwierModels.Find(id);
if (interwierModel == null)
{
return HttpNotFound();
}
return View(interwierModel);
}
// POST: Interwier/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Email,Telephone,Birthday,City,Salary,English,Status")] Interwier interwierModel)
{
if (ModelState.IsValid)
{
db.Entry(interwierModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Incoming");
}
return View(interwierModel);
}
And here is how it looks on View 这是它在View上的外观
<div class="form-group" style="padding-left: 515px;">
<div class="col-md-10">
@Html.DropDownListFor(model => model.Status, new[]
{
new SelectListItem() {Text = "Подтвердить", Value = "Одобрено"},
new SelectListItem() {Text = "Отправить в архив", Value = "Архив"},
}, "Статус", new { @class = "form-control" })
</div>
</div>
When I click submit button I have this error 单击提交按钮时出现此错误
How I can handle this? 我该如何处理?
You need to include Interwier_id
property as hidden field. 您需要包括
Interwier_id
属性作为隐藏字段。 Use HiddenFor
extension method. 使用
HiddenFor
扩展方法。 You don't post the ID to server that's why EF cannot find the entity in DB. 您没有将ID发布到服务器,这就是EF无法在DB中找到实体的原因。 Assuming the code below is within the
BeginForm
block. 假设下面的代码在
BeginForm
块中。
@Html.DropDownListFor(model => model.Interwier_id)
<div class="form-group" style="padding-left: 515px;">
<div class="col-md-10">
@Html.DropDownListFor(model => model.Status, new[]
{
new SelectListItem() {Text = "Подтвердить", Value = "Одобрено"},
new SelectListItem() {Text = "Отправить в архив", Value = "Архив"},
}, "Статус", new { @class = "form-control" })
</div>
</div>
And also update properties that you bind from Id to Interwier_id: 并更新从ID绑定到Interwier_id的属性:
[Bind(Include = "Interwier_id,...")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.