如何解决这个修改控制器?

时间:2014-01-09 12:42:36

标签: c# asp.net-mvc-3 entity-framework asp.net-mvc-4

每一件事都是对的,我所有的价值观都让我得到了带有值的新模型来修改它但是最后一步出了问题 我的模型验证状态是真的 这是我的帖子控制器

[HttpPost]       
    public ActionResult Edit(Problem problem, HttpPostedFileBase fileUpload)
    {
        Problem Edproblem = db.Problems.Find(problem.Id);
        ViewBag.ATMId = new SelectList(db.ATMs, "Id", "AtmNumber", Edproblem.ATM.AtmNumber);
        ViewBag.UserId = WebSecurity.GetUserId(User.Identity.Name);
        ViewBag.ProblemTypeId = new SelectList(db.ProblemTypes, "Id", "Name", Edproblem.ProblemTypeId);
        var bankId = from e in db.ATMs where e.Id == problem.ATMId select e;
        ViewBag.BankId = new SelectList(db.Banks, "Id", "Name", bankId);
        ViewBag.AreaId = new SelectList(db.Areas, "Id", "Name", db.Areas.Where(p => p.Id == Edproblem.ATM.AreaId));
        ViewBag.GovId = new SelectList(db.Governates, "Id", "Name", Edproblem.ATM.Area.GovernateId);

        Problem beforeEdit = db.Problems.AsNoTracking().First(p => p.Id == problem.Id);

        if (ModelState.IsValid)
        {
            DateTime now = DateTime.Now;

            if (!Directory.Exists(Server.MapPath("~/UploadImages/" + now.ToString("yyyy_MM/"))))
            {
                Directory.CreateDirectory(Server.MapPath("~/UploadImages/" + now.ToString("yyyy_MM/")));
            }
            if (!Directory.Exists(Server.MapPath("~/UploadImages/" + now.ToString("yyyy_MM/") + Edproblem.ATMId.ToString())))
            {
                Directory.CreateDirectory(Server.MapPath("~/UploadImages/" + now.ToString("yyyy_MM/") + Edproblem.ATMId.ToString()));
            }
            if (!Directory.Exists(Server.MapPath("~/UploadImages/" + now.ToString("yyyy_MM/") + Edproblem.ATMId.ToString() + now.ToString("/dd/"))))
            {
                Directory.CreateDirectory(Server.MapPath("~/UploadImages/" + now.ToString("yyyy_MM/") + Edproblem.ATMId.ToString() + now.ToString("/dd/")));
            }

            var image = WebImage.GetImageFromRequest();

            if (fileUpload != null)
            {
                string fileName = fileUpload.FileName.ToString();
                fileName = Guid.NewGuid().ToString() + fileName.Substring(fileName.LastIndexOf("."));
                fileName = now.ToString("yyyy_MM/") + problem.ATMId.ToString() + "/" + now.ToString("dd/") + fileName;
                if (fileUpload != null && fileUpload.ContentLength > 0)
                    fileUpload.SaveAs(Server.MapPath("~/UploadImages/" + image));

                image.Save(Server.MapPath("/UploadImages/" + fileName));                   

                problem.ImagePath = fileName;
            }
            else
            {                    
                if (problem.ImagePath == null)
                {
                    var getPath = (from e in db.Problems 
                                  where e.Id == problem.Id
                                  select e.ImagePath).ToList();
                    problem.ImagePath = getPath[0].ToString();
                }
            }
            problem.UserId = WebSecurity.GetUserId(User.Identity.Name);                
            db.Entry(problem).State = EntityState.Modified;                
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(problem);
    }

这是错误,因为它看起来完全正确:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

1 个答案:

答案 0 :(得分:1)

您正在将一个Problem对象加载到变量beforeEdit中,并且没有进一步使用它。

如果您尝试使用参数中的新数据更新beforeEdit,并保存该数据而不是从视图返回的对象。