我对MVC很陌生。我从数据库中检索了值,并在视图中显示了它,我想进行更改并更新现有值。检索到的值没有问题,但是当我创建保存时,它将在db.changes行返回错误:


  存储更新,插入或删除语句影响了意外
  行数(0)。自从以来,实体可能已被修改或删除
  实体已加载。刷新ObjectStateManager条目。


我已经尝试了Edit.cshtml中包括@ Html.HiddenFor(m => m.Question_ID)的所有内容,但还是没有运气。

我的CreateQuestionViewModel:

public class CreateQuestionViewModel
    {
        [HiddenInput(DisplayValue = false)]
        public int Question_ID { get; set; }

        [HiddenInput(DisplayValue=false)]
        public int Survey_ID { get; set; }

        [DisplayName("Question Title")]
        public string Qext_Text { get; set; }

        [DisplayName("Question Type")]
        public string Question_Type { get; set; }

        public bool Mandatory { get; set; }

        public string Language { get; set; }
    }
}


我的编辑控制器:

  [HttpGet]
            public ActionResult Edit(int Question_ID = 0)
            {
                var viewModel = new CreateQuestionViewModel();
                viewModel.Question_ID = Question_ID;

                if (ModelState.IsValid)
                {
                    SURV_Question_Model surv_question_model = db.SURV_Question_Model.Find(Question_ID);
                    viewModel.Mandatory = surv_question_model.Question_Mandatory;
                    viewModel.Question_Type = surv_question_model.Question_Type;

                    SURV_Question_Ext_Model surv_question_ext_model = db.SURV_Question_Ext_Model.FirstOrDefault(x => x.Qext_Question_ID.Equals(Question_ID));
                    viewModel.Qext_Text = surv_question_ext_model.Qext_Text;
                    viewModel.Language= surv_question_ext_model.Qext_Language;

                }
                return View(viewModel);
            }

        [HttpPost]
                public ActionResult Edit(CreateQuestionViewModel viewModel)
                {
                    if (ModelState.IsValid)
                    {
                        var question = new SURV_Question_Model();
                        question.Question_Mandatory = viewModel.Mandatory;
                        question.Question_Type = viewModel.Question_Type;

                        db.Entry(question).State = EntityState.Modified;
                        db.SaveChanges();

                        var question_ext = new SURV_Question_Ext_Model();
                        question_ext.Qext_Text = viewModel.Qext_Text;
                        question_ext.Qext_Language = viewModel.Language;

                        db.Entry(question_ext).State = EntityState.Modified;
                        db.SaveChanges();


                        return RedirectToAction("Edit", "SURV_Main", new { id = viewModel.Survey_ID });

                    }


请给我一些指导。
谢谢!

最佳答案

您正在创建新的questionquestion_ext并从传递的视图模型中分配值。因此,如果您尝试保存更改,它将保存为新行,但在此之前您将其中的entitystate指定为modified,这样系统就无法进行修改。这就是错误发生的原因。
 因此,首先,而不是创建一个新的创建这样的事情,然后尝试

     var question = db.createviewquestionmodel.where
    (d=> d.question_id == viewmodel.question_id).firstordefault();

    question.Question_Mandatory = viewModel.Mandatory;
   question.Question_Type = viewModel.Question_Type;
   db.Entry(question).State = EntityState.Modified;
   db.SaveChanges();


然后将值分配给问题并尝试以编辑模式保存

关于c# - 存储更新,插入或删除语句影响了意外的行数(0)。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30283027/

10-10 19:03