错误消息: ObjectStateManager中已存在具有相同键的对象。 ObjectStateManager不能使用同一个键跟踪多个对象。如何在编辑空值的情况下从DB获取值
如果fileupload具有空值(不会更改),我想从数据库获取Image Url。 我的意思是如果我改变smallImage而不改变LargeImage,那么它应该从数据库中获取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);
}
谢谢。
是否使用实体框架? – tomasmcguinness