【发布时间】:2016-01-23 21:08:57
【问题描述】:
在我的 POST 编辑函数中,我的视图模型包含我想要更新的游戏和我想要添加到游戏中的平台 ID 列表。
使用此代码,我能够将平台添加到我的游戏中,但无法删除它们。我在最后放了一个断点,肯定看到 viewModel.Game.Platforms 只有我选择的内容,但它没有在我的游戏列表中更新。
如果我添加几个平台并同时删除一些。添加了新平台,但没有删除任何平台。
public ActionResult Edit(GameViewModel viewModel)
{
if (ModelState.IsValid)
{
List<Platform> platforms = new List<Platform>();
foreach (var id in viewModel.PostedPlatforms.PlatformIds)
{
platforms.Add(db.Platforms.Find(Int32.Parse(id)));
}
db.Games.Attach(viewModel.Game);
viewModel.Game.Platforms = platforms;
db.Entry(viewModel.Game).State = EntityState.Modified;
UpdateModel(viewModel.Game);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(viewModel.Game);
}
模型类是
public class Game
{
public int GameId { get; set; }
public string Title { get; set; }
public List<Platform> Platforms { get; set; }
}
public class Platform
{
public int PlatformId { get; set; }
public string Name { get; set; }
public List<Game> Games { get; set; }
}
根据 ourmandave 的建议,我得到了这段代码,虽然确实改变了平台选择,但它每次都会创建一个新的游戏条目,效率低下,并且还会增加内容的 id,从而弄乱书签。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(GameViewModel viewModel)
{
if (ModelState.IsValid)
{
List<Platform> platforms = new List<Platform>();
if(viewModel.PostedPlatforms != null)
{
foreach (var id in viewModel.PostedPlatforms.PlatformIds)
{
platforms.Add(db.Platforms.Find(Int32.Parse(id)));
}
}
db.Games.Remove(db.Games.Find(viewModel.Game.PostId));
db.SaveChanges();
viewModel.Game.Platforms = platforms;
db.Games.Add(viewModel.Game);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(viewModel.Game);
}
【问题讨论】:
-
删除项目的代码在哪里?
-
在我的编辑功能中,我从我选择的所有平台创建一个新的 List
并将 Game.Platforms 设置为那个。新列表没有 Game.Platforms 中的某些平台。这不应该意味着这些平台不再存在于 Game.Platforms 中吗? -
我认为您必须在它们上调用 Remove 或 DeleteObject 以在上下文中标记它们,因此当您 SaveChanges 时它将生成 Delete sql per this answer。另请参阅此页面上的Entity states and SaveChanges。
-
我没有看到仅在我的游戏平台列表中调用它的方法。如果我必须删除我的游戏并在每次我想更新它时重新添加它,这似乎相当尴尬。
标签: c# asp.net-mvc entity-framework entity-framework-5