在这个论坛的一些出色帮助下,任何人都可以确认这是否是更新数据库中数据的最佳方式,在 MVC 控制器中,多个记录/模型一次回发?
[HttpPost]
public ActionResult Edit(ObjectivesEdit model)
{
if (model.Objectives != null)
{
// model will have several records posted back - so loop through each one, and update the database
foreach (var item in model.Objectives)
{
// find the database row
Objective objective = db.objectives.Find(item.ID);
// Set the database row to the posted values
objective.objective = item.objective;
objective.score = item.score;
objective.possscore = item.possscore;
objective.comments = item.comments;
}
// Save the changes to the database
db.SaveChanges();
}
return View(model);
}
我认为可能有更好的工作方式的部分是:
objective.objective = item.objective;
objective.score = item.score;
objective.possscore = item.possscore;
objective.comments = item.comments;
可以用更精简的东西代替,还是这样做的方式?