我的网络应用是关于在线资讯池的,它有Model.Question
和Model.Answer
。
有问题我有Id
,Text
(问题的价值),List of Answers
,StartDate
,EndDate
以及active
。
在答案模型中,我有Id
,QuestionId
,Text
(答案的价值)和Votes
(现在不想使用它)
当我从问题(ID)中检索值时,我遇到了问题,因此Answer.QuestionId
有ID。然后我需要将Model.Answer.Text
添加到Question.Answer
;
这是我的控制器:
public ActionResult AddAnswer(int id)
{
//return View();
using (var poolDbContext = new PoolContext())
{
Answer answer = poolDbContext.Answers.Find(id);
return View(answer);
}
}
[HttpPost, ActionName("AddAnswer")]
[ValidateAntiForgeryToken]
public ActionResult AddAnswerPost([Bind("Id, Answers")] Question question, Answer answer)
{
try
{
if (ModelState.IsValid)
{
using (var poolDbContext = new PoolContext())
{
answer.QuestionId = question.Id;
poolDbContext.Answers.Add(answer);
question.Answers.Add(answer);
repository.Add(question);
repository.SaveChanges();
return RedirectToAction("Index");
}
}
}
catch (DbUpdateException /* ex */)
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists " +
"see your system administrator.");
}
return View(answer);
}
答案 0 :(得分:0)
所以,你可以改变你的代码(为了简洁而省略了控制器的其余部分),如下所示:
using (var poolDbContext = new PoolContext())
{
// get the question from db, also including the answers
var questionFromDb = poolDbContext.Questions
.Include(q => q.Answers)
.Single(q => q.Id == question.Id);
// good idea to validate if the question exists..
if (questionFromDb == null)
throw new ArgumentException("Question not found.");
// if answer is a new answer and not an update,
// then just add it to the current answers.
answer.QuestionId = questionFromDb.Id;
questionFromDb.Answers.Add(answer);
// dbContext is tracking changes, so you don't need to add to the dbSet.
repository.SaveChanges();
return RedirectToAction("Index");
}
但是......这假定了一些事情:
Answer
上的ID是一个带有自动增量设置的int。如果是这种情况那么它将起作用。例如,如果它是Guid
,那么您需要先创建Id,所以请记住这一点。repository
,我不知道它是什么。但是,既然你正在那里使用dbContext,我没有看到它的使用,所以在我的解决方案中完全被忽略并且......给你一些其他建议:
DbContext
。这样您就不必在控制器中实例化它,它的生命周期由DI系统管理(if you configure it correctly)希望它有助于澄清一些事情。