として定義されたビューモデルで
public ActionResult Save(Survey survey, List<Category> category, List<Question> question, List<Answer> answer)
{
//var categoryInDb = _context.Categories.Single(m => m.CategoryId == category.CategoryId);
//categoryInDb.CategoryDescription = category.CategoryDescription;
//Updated loop.
foreach (var categorytoAdd in category)
{
foreach (var categoryInDb in _context.Categories.Where(m => m.CategoryId == categorytoAdd.CategoryId))
categoryInDb.CategoryDescription = categorytoAdd.CategoryDescription;
}
var questionInDb = _context.Questions.Single(m => m.QuestionId == question.QuestionId);
questionInDb.QuestionText = question.QuestionText;
_context.SaveChanges();
return RedirectToAction("Index", "Surveys");
}
。 複数のアイテムを使用する場合は、Where
(もちろんIEnumerable
を返します)を使用します。今コレクションがあるので、アイテムを変更/アクセス/使用するには、foreach
以上にする必要があります。
ので:
foreach (var categoryInDb in _context.Categories.Where(m => m.CategoryId == category.CategoryId))
categoryInDb.CategoryDescription = category.CategoryDescription;
sovempが気づいたとして、あなたは、あなたが開始するための複数のレコードを持っていた場合Single
が実際にスローされますされています。したがって、結果が複数返されるように全体のクエリを修正するまでは、これは何も行いません。
'Save'メソッドは1つの質問/回答のみを受け取るので、どのように複数のアイテムを更新すると思いますか? – Phil1970