我对MVC很陌生。 我从数据库中检索了值并在视图中显示它,我想进行更改并更新现有值。 检索到的值没有问题,但是当我创建save时,它会在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 }); }请给我一些指导。 谢谢!
i'm quite new to MVC. I was retrieved the value from database and show it in view, and i want to made changes and updated existing value. There is no problem on retrieved value, but when i create save, it returns the error at the db.changes line:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
I have try everything which is include the @Html.HiddenFor(m => m.Question_ID) in the Edit.cshtml but still no luck.
My 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; } } }My Edit Controller:
[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 }); }Please give some guidance to me. Thanks!
最满意答案
您正在创建一个新question和question_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();然后将值分配给问题并尝试使用编辑模式进行保存
You are creating a new question and question_ext and assigning value from the viewmodel passed. so if you try to save changes it will save as new row but before that you specify entitystate of them as modified so system can't modify.That why the error is coming. So first instead of creating a new create something like this and try
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();then assign the values to question and try to save with edit mode
更多推荐
发布评论