【问题标题】:Could not Update data using asp mvc Entity-Framework无法使用 asp mvc Entity-Framework 更新数据
【发布时间】:2016-05-16 14:18:28
【问题描述】:

作为 ASP .net MVC 的初学者,我正在尝试使用 Entity Framework 更新数据库,但 id 始终为空,这使得模型状态始终无效 这是控制器的代码

【问题讨论】:

  • 你需要在问题中包含代码,而不是它的图像

标签: .net asp.net-mvc entity-framework model-view-controller


【解决方案1】:

您的 Id 等于 null,因为模型绑定器未从您发布的数据中找到值。

要解决这个问题,您必须将Id 作为隐藏字段添加到您的视图中。在您的 Edit.cshtml 中只需添加以下行:

@Html.HiddenFor(model => model.Id)

【讨论】:

  • 它始终为空:/
【解决方案2】:

这样试试 ->

public ActionResult Edit([Bind(Include = "Field1,Field2,Field3...")] Etudiant et)
    {
        if (ModelState.IsValid)
        {
            db.Entry(et).State = EntityState.Modified;

            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2022-12-07
    • 2020-02-13
    • 2020-08-05
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多