1

ここにあるソリューションを実装しようとしていますが、「Users」クラスを「EditUserViewModel」クラス (Users クラスよりもフィールドが少ないクラス) に置き換えて、この行に到達したら

db.Entry(users).State = EntityState.Modified;

このコードで

    [HttpPost]
    public ActionResult Edit(EditUserViewModel users)
    {
        if (ModelState.IsValid)
        {
            db.Entry(users).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(users);
    }

エラーが発生します

The entity type EditUserViewModel is not part of the model for the current context.

このエラーは、DBContext が EditUserViewModel クラスではなく Users クラスを使用しているためだと推測しています。手順が不足していますか、またはこれを修正する方法はありますか?

4

1 に答える 1

1

MV のデータを Model クラスにマージする必要があります。

if (ModelState.IsValid)
{
    var existingUser = db.Users.Single(p => /* query to find your user */);

    existingUser.From = user.From;
    existingUser.CC = user.CC;
    // etc.

    db.SaveChanges();

    return RedirectToAction("Index");
}
于 2012-09-29T04:58:57.647 に答える