使用MVC创建Player类型的数据库条目:
[HttpPost]
public ActionResult Create(FormCollection fc, Player player)
{
players.Players.InsertOnSubmit(player);
players.SubmitChanges();
Errors errors;
if (!IsValid(player, out errors))
{
ViewBag.Errors = errors;
return RedirectToAction("Edit", player);
}
return Redirect("/Home/Players");
}
[HttpPost]
public ActionResult Edit(FormCollection fc, Player player)
{
players.SubmitChanges();
return Redirect("/Home/Players");
}
我的问题是Edit方法中的players.SubmitChanges()
不会更改数据库中的任何内容。在“创建”中使用InsertObSubmit
。我可以用另一种方式吗?
答案 0 :(得分:1)
[HttpPost]
public ActionResult Edit(FormCollection fc, Player player)
{
// I guess you are forgetting this
players.Players.AttachAsModified(player)
players.SubmitChanges();
return Redirect("/Home/Players");
}
答案 1 :(得分:0)
当您获得播放器值作为编辑操作的输入参数时,您应该首先相应地修改您的players
集合 - 这不会自动发生 - 然后才会players.SubmitChanges();