Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在编辑具有空值的条目时从DB获取值_Asp.net Mvc_Asp.net Mvc 3_Entity Framework - Fatal编程技术网

Asp.net mvc 如何在编辑具有空值的条目时从DB获取值

Asp.net mvc 如何在编辑具有空值的条目时从DB获取值,asp.net-mvc,asp.net-mvc-3,entity-framework,Asp.net Mvc,Asp.net Mvc 3,Entity Framework,错误消息: ObjectStateManager中已存在具有相同密钥的对象。ObjectStateManager无法跟踪具有相同密钥的多个对象 如果fileupload有空值(不更改),我想从数据库中获取图像Url。 我的意思是,如果我改变了smallImage而没有改变LargeImage,那么它应该从DB中得到LargeImage值 [HttpPost] public ActionResult Edit(Blog blog, HttpPostedFileBase smallImage

错误消息: ObjectStateManager中已存在具有相同密钥的对象。ObjectStateManager无法跟踪具有相同密钥的多个对象

如果fileupload有空值(不更改),我想从数据库中获取图像Url。 我的意思是,如果我改变了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);
    }

谢谢。

看起来这里的问题是,您两次加载同一个博客

改为加载一次,如下所示:

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

你们都在加载博客的副本

 db.Blogs.Find(blog.ID)
以及将具有相同id的另一个附加到上下文

db.Entry(blog).State = EntityState.Modified;
这意味着你在上下文中有两个相同的博客副本(不允许)

我建议用viewmodel替换回发的,比如

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

您正在使用实体框架吗?