【问题标题】:How to solve This modify Controller?如何解决这个修改控制器?
【发布时间】:2014-01-28 00:37:58
【问题描述】:

一切正常 我的模型验证状态为真 这是我的帖子控制器

[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.

【问题讨论】:

  • 如果您不打算使用 Problem 对象,为什么还要在变量 beforeEdit 中加载它?
  • 我正在尝试从存储的现有数据中获取旧值,但失败且错误消息没有变化,您可以说是无用的步骤
  • 你不能用新数据填充 beforeEdit,然后保存那个吗?在没有 AsNoTracking() 的情况下加载它,然后填充 UserId、imagepath 等。
  • 非常感谢,我做了一些改变,它就像你所说的那样工作
  • 太棒了!在下面总结了一个简短的答案。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2023-01-26
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2020-06-12
    相关资源
    最近更新 更多