自实体加载后,实体可能已被修改或删除。 (ASP.NET MVC)

时间:2017-03-19 10:15:27

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-4

我尝试从SelectedList

更新表中的一个属性

这是我的模特

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

我需要更新Status属性。

这是控制器中的编辑操作

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

以下是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>

当我点击提交按钮时出现此错误

enter image description here

我该如何处理?

1 个答案:

答案 0 :(得分:0)

您需要将Interwier_id属性包含为隐藏字段。使用HiddenFor扩展方法。您不会将ID发布到服务器,这就是EF无法在DB中找到实体的原因。假设下面的代码在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>

还会更新从Id绑定到Interwier_id的属性:

[Bind(Include = "Interwier_id,...")]