ASP.NET 核心 MVC:在发布编辑方法之前删除多对多条目
ASP.NET Core MVC : remove many-to-many entries before posting Edit method
我在 Project
和 Member
之间有一个多对多关系,通过连接 table ProjectMembers
实现。在视图中,为了添加特定的项目成员,我使用多个 select 将 MemberId
存储在 IEnumerable<int> SelectedMembers
.
中
一切正常,除了我只能用新成员更新(编辑)项目(未 selected 并且之前属于数据库的成员保持不变)。在 post 更新成员集之前,我需要帮助删除 ProjectMember
中连接到特定 ProjectId
的现有成员。我已经尝试了很多,但到目前为止没有任何效果。真的任何建议将不胜感激。
这是我在ProjectController
中的Edit
post方法:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(int? id, CreateProjectViewModel viewmodel)
{
Project project = _context.Project
.Single(m => m.ProjectId == id);
project.Name = viewmodel.Name;
project.Budget = viewmodel.Budget;
project.BusinessCase = viewmodel.BusinessCase;
project.StartDate = viewmodel.StartDate;
project.FinishDate = viewmodel.FinishDate;
project.ClientId = viewmodel.ClientId;
// here I need the method to remove the existing instances of Member in ProjectMember
// part of code below is validation that there will be no conflict of PrimaryKeys on ProjectMember, probably can be removed once the Remove method is implemented
foreach (var selectedId in viewmodel.SelectedMembers)
{
var projectID = project.ProjectId;
var memberID = selectedId;
IList<ProjectMember> existingItems = _context.ProjectMembers
.Where(cm => cm.MemberId == memberID)
.Where(cm => cm.ProjectId == projectID).ToList();
if (existingItems.Count == 0)
{
_context.ProjectMembers.Add(new ProjectMember
{
ProjectId = project.ProjectId,
MemberId = selectedId,
});
}
}
_context.SaveChanges();
return RedirectToAction("Index");
}
更新:
基于类似的线程,我想出了以下内容,在使用 POST 编辑将新行添加到 ProjectMembers
之前添加:
var project = _context.Project.Include(a => a.ProjectMembers)
.SingleOrDefault(m => m.ProjectId == id);
if (project != null)
{
foreach (var projectMember in project.ProjectMembers
.Where(at => viewmodel.SelectedMembers.Contains(at.MemberId)).ToList())
{
project.ProjectMembers.Remove(projectMember);
}
_context.SaveChanges();
}
不幸的是,ProjectMembers
中应该删除的条目继续存在,任何人都可以建议应该更改什么吗?
知道了。显然是一种更简单的方法。下面的代码是在新的通过之前删除整个:
var project = _context.Project.Include(a => a.ProjectMembers)
.SingleOrDefault(m => m.ProjectId == id);
foreach (var member in project.ProjectMembers.ToArray())
{
project.ProjectMembers.Remove(member);
}
我在 Project
和 Member
之间有一个多对多关系,通过连接 table ProjectMembers
实现。在视图中,为了添加特定的项目成员,我使用多个 select 将 MemberId
存储在 IEnumerable<int> SelectedMembers
.
一切正常,除了我只能用新成员更新(编辑)项目(未 selected 并且之前属于数据库的成员保持不变)。在 post 更新成员集之前,我需要帮助删除 ProjectMember
中连接到特定 ProjectId
的现有成员。我已经尝试了很多,但到目前为止没有任何效果。真的任何建议将不胜感激。
这是我在ProjectController
中的Edit
post方法:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(int? id, CreateProjectViewModel viewmodel)
{
Project project = _context.Project
.Single(m => m.ProjectId == id);
project.Name = viewmodel.Name;
project.Budget = viewmodel.Budget;
project.BusinessCase = viewmodel.BusinessCase;
project.StartDate = viewmodel.StartDate;
project.FinishDate = viewmodel.FinishDate;
project.ClientId = viewmodel.ClientId;
// here I need the method to remove the existing instances of Member in ProjectMember
// part of code below is validation that there will be no conflict of PrimaryKeys on ProjectMember, probably can be removed once the Remove method is implemented
foreach (var selectedId in viewmodel.SelectedMembers)
{
var projectID = project.ProjectId;
var memberID = selectedId;
IList<ProjectMember> existingItems = _context.ProjectMembers
.Where(cm => cm.MemberId == memberID)
.Where(cm => cm.ProjectId == projectID).ToList();
if (existingItems.Count == 0)
{
_context.ProjectMembers.Add(new ProjectMember
{
ProjectId = project.ProjectId,
MemberId = selectedId,
});
}
}
_context.SaveChanges();
return RedirectToAction("Index");
}
更新:
基于类似的线程,我想出了以下内容,在使用 POST 编辑将新行添加到 ProjectMembers
之前添加:
var project = _context.Project.Include(a => a.ProjectMembers)
.SingleOrDefault(m => m.ProjectId == id);
if (project != null)
{
foreach (var projectMember in project.ProjectMembers
.Where(at => viewmodel.SelectedMembers.Contains(at.MemberId)).ToList())
{
project.ProjectMembers.Remove(projectMember);
}
_context.SaveChanges();
}
不幸的是,ProjectMembers
中应该删除的条目继续存在,任何人都可以建议应该更改什么吗?
知道了。显然是一种更简单的方法。下面的代码是在新的通过之前删除整个:
var project = _context.Project.Include(a => a.ProjectMembers)
.SingleOrDefault(m => m.ProjectId == id);
foreach (var member in project.ProjectMembers.ToArray())
{
project.ProjectMembers.Remove(member);
}