I'm having some issues in updating and inserting records using ASP.NET MVC and Entity Framework.
我在使用ASP更新和插入记录方面有一些问题。NET MVC和实体框架。
I have a form (which is a report) that is dynamically created and can have any amount of questions. I'm trying to allow the user to edit the report and submit the changes so that it is updated in the database.
我有一个表单(一个报告),它是动态创建的,可以有任何问题。我试图允许用户编辑报表并提交更改,以便在数据库中更新报表。
I am retrieving the report to be edited from the database via a repository then setting it to an instance of ModeratorReport
. I'm then changing the value of the properties and using db.SaveChanges
to save the changes to the database.
我正在通过存储库从数据库检索要编辑的报表,然后将其设置为ModeratorReport的一个实例。然后改变属性的值并使用db。SaveChanges将更改保存到数据库。
The problem is that it is not saving the changes.
问题是它没有保存更改。
Please could someone advise me on what I am doing wrong?
请谁能就我做错了什么事给我出主意?
Here is the Edit
Action:
以下是编辑动作:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(FormCollection formCollection, int moderatorReportId, string status)
{
ModeratorReport reportToEdit = repository.GetModeratorReportById(moderatorReportId);
List<QuestionAnswer> originalReportAnswers = repository.GetAllModeratorReportAnswers(moderatorReportId, status).ToList();
foreach (QuestionAnswer answer in originalReportAnswers) {
reportToEdit.QuestionAnswers.Remove(answer);
}
int sectionID;
int questionID;
foreach (string key in formCollection.AllKeys)
{
var value = formCollection[key.ToString()];
Match m = Regex.Match(key, "section(\\d+)_question(\\d+)");
if (m.Success) {
QuestionAnswer newAnswer = new QuestionAnswer();
sectionID = Convert.ToInt16(m.Groups[1].Value.ToString());
questionID = Convert.ToInt16(m.Groups[2].Value.ToString());
newAnswer.ModeratorReportID = moderatorReportId;
newAnswer.SectionID = sectionID;
newAnswer.QuestionID = questionID;
newAnswer.Answer = value;
newAnswer.Status = "SAVED";
reportToEdit.QuestionAnswers.Add(newAnswer);
}
}
reportToEdit.Status = "SAVED";
AuditItem auditItem = new AuditItem();
auditItem.ModeratorReportID = moderatorReportId;
auditItem.Status = "SAVED";
auditItem.AuditDate = DateTime.Now;
auditItem.Description = "The Moderator report..."
auditItem.UserID = User.Identity.Name;
reportToEdit.Audit.Add(auditItem);
db.SaveChanges();
return RedirectToAction("Details", new { id = moderatorReportId });
}
1 个解决方案
#1
1
The problem looks like you're just not setting reportToEdit
's EntityState to modified. Like so:
问题似乎是您没有将reportToEdit的EntityState设置为modified。像这样:
reportToEdit.Audit.Add(auditItem);
reportToEdit.EntityState = EntityState.Modified;
db.SaveChanges();
For more information about the EntityState enumeration, see this MSDN article.
有关EntityState枚举的更多信息,请参见本文。
#1
1
The problem looks like you're just not setting reportToEdit
's EntityState to modified. Like so:
问题似乎是您没有将reportToEdit的EntityState设置为modified。像这样:
reportToEdit.Audit.Add(auditItem);
reportToEdit.EntityState = EntityState.Modified;
db.SaveChanges();
For more information about the EntityState enumeration, see this MSDN article.
有关EntityState枚举的更多信息,请参见本文。