0

エラーメッセージ: 同じキーを持つオブジェクトがObjectStateManagerにすでに存在します。ObjectStateManagerは、同じキーを持つ複数のオブジェクトを追跡できません。

fileuploadの値がnull(変更されていない)の場合、データベースからImageUrlを取得したい。つまり、smallImageを変更し、LargeImageを変更しない場合、DBからlargeImage値を取得する必要があります。

[HttpPost]
    public ActionResult Edit(Blog blog, HttpPostedFileBase smallImage, HttpPostedFileBase largeImage)
    {
        if (ModelState.IsValid)
        {
            if (smallImage != null)
            {
                blog.SmallImage = smallImage.ContentLength + "_" + smallImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), smallImage.ContentLength + "_" + smallImage.FileName);
                smallImage.SaveAs(filepath);
            }
            else
            {
                blog.SmallImage = db.Blogs.Find(blog.ID).SmallImage;
            }
            if (largeImage != null)
            {
                blog.LargeImage = largeImage.ContentLength + "_" + largeImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), largeImage.ContentLength + "_" + largeImage.FileName);
                largeImage.SaveAs(filepath);
            }
            else
            {
                blog.LargeImage = db.Blogs.Find(blog.ID).LargeImage;
            }
            blog.PostDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            db.Entry(blog).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(blog);
    }

ありがとうございました。

4

2 に答える 2

0

あなたは両方ともブログのコピーを読み込んでいます

 db.Blogs.Find(blog.ID)

同じIDを持つ別のコンテキストにアタッチします

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

つまり、コンテキスト内に同じブログのコピーが2つあることを意味します(許可されていません)。

代わりに、投稿されたものをビューモデルに置き換えることをお勧めします。

public ActionResult Edit(BlogViewModel viewModel, HttpPostedFileBase smallImage, HttpPostedFileBase largeImage)
{
    if (!ModelState.IsValid)
    {
        return View(viewModel);
    }
        var blog =  db.Blogs.Find(viewModel.ID);
        if (smallImage != null)
        {
            blog.SmallImage = smallImage.ContentLength + "_" + smallImage.FileName;
            string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), smallImage.ContentLength + "_" + smallImage.FileName);
            smallImage.SaveAs(filepath);
        }

        if (largeImage != null)
        {
            blog.LargeImage = largeImage.ContentLength + "_" + largeImage.FileName;
            string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), largeImage.ContentLength + "_" + largeImage.FileName);
            largeImage.SaveAs(filepath);
        }


        blog.Title = viewModel.Title;
        blog.Body = viewModel.Body; //etc

        db.SaveChanges();
        return RedirectToAction("Index");
}
于 2012-10-08T08:03:15.727 に答える
0

ここでの問題のように見えるのは、同じブログを 2 回読み込んでいることです。

代わりに、次のように一度ロードします。

Blog existingBlog = db.Blogs.Find(blog.ID);
if (smallImage != null)
            {
                blog.SmallImage = smallImage.ContentLength + 
                                        "_" + smallImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"),
                           smallImage.ContentLength + "_" + smallImage.FileName);
                smallImage.SaveAs(filepath);
            }
            else
            {
                blog.SmallImage = existingBlog.SmallImage;
            }
            if (largeImage != null)
            {
                blog.LargeImage = largeImage.ContentLength + "_" +
                                     largeImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"),
                                                 largeImage.ContentLength + "_" +
                                                 largeImage.FileName);
                largeImage.SaveAs(filepath);
            }
            else
            {
                blog.LargeImage = existingBlog.LargeImage;
            }
于 2012-10-08T07:56:32.513 に答える