I have two tables “Project” and “Offices” and I want to implement the “Many to Many” relationship which produces “ProjectManagement” table.
I want to define the responsible offices when I create the project, I implemented the create action but I have a problem in [HttpPost]edit action, I solved it by deleting all the previous records in the “ProjectManagement” table which related with the current edited project then resave the new edit but it seems not well.
Project.cs
public class Project
{
public int Id { get; set; }
public string ProjectCode { get; set; }
public string Donor { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndtDate { get; set; }
}
Office.cs
public class Office
{
public int Id { get; set; }
public int RegionId { get; set; }
public string OfficeName { get; set; }
public string OfficeAddress { get; set; }
public string Latitude_y { get; set; }
public string Longitude_x { get; set; }
public virtual Region Region { get; set; }
public bool Selected { get; set; }
}
ProjectManagement.cs
public class ProjectManagement
{
public int Id { get; set; }
public int ProjectId { get; set; }
public int OfficeId { get; set; }
public virtual Project Project { get; set; }
public virtual Office Office { get; set; }
}
ProjectViewModel.cs
public class ProjectViewModel
{
public int Id { get; set; }
public string ProjectCode { get; set; }
public string Donor { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndtDate { get; set; }
public string Notes { get; set; }
public IList<SelectListItem> Offices { get; set; }
}
// GET: Project/Create
public IActionResult Create()
{
var offices = _office.Entity.GetAll();
var item = offices.Select(x => new SelectListItem()
{
Text = x.OfficeName,
Value = x.Id.ToString()
}).ToList();
var projectVM = new ProjectViewModel()
{
Offices = item
};
return View(projectVM);
}
// POST: Project/Create
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(ProjectViewModel model)
{
if (ModelState.IsValid)
{
Project project = new Project
{
ProjectCode = model.ProjectCode,
Donor = model.Donor,
StartDate = model.StartDate,
EndtDate = model.EndtDate
};
_project.Entity.Insert(project);
_project.Save();
var officeIds = model.Offices.Where(x => x.Selected).Select(y => y.Value);
foreach (var id in officeIds)
{
ProjectManagement pm = new ProjectManagement
{
OfficeId = int.Parse(id),
ProjectId = project.Id
};
_projectManagement.Entity.Insert(pm);
_projectManagement.Save();
}
return RedirectToAction(nameof(Index));
}
return View(model);
}
// GET: Project/Edit/5
public IActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var project = _project.Entity.GetById(id);
if (project == null)
{
return NotFound();
}
var selectedoffices = _projectManagement.Entity.GetAll().Where(a => a.ProjectId == id);
var offices = _office.Entity.GetAll();
foreach (var i in selectedoffices)
{
i.Office.Selected = true;
}
var item = offices.Select(x => new SelectListItem()
{
Text = x.OfficeName,
Value = x.Id.ToString(),
Selected = x.Selected
}).ToList();
ProjectViewModel projectViewModel = new ProjectViewModel
{
Id = project.Id,
ProjectCode = project.ProjectCode,
Donor = project.Donor,
StartDate = project.StartDate,
EndtDate = project.EndtDate,
Offices = item
};
return View(projectViewModel);
}
// POST: Project/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(int id, ProjectViewModel model)
{
if (id != model.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
Project project = new Project
{
Id = model.Id,
ProjectCode = model.ProjectCode,
Donor = model.Donor,
StartDate = model.StartDate,
EndtDate = model.EndtDate
};
_project.Entity.Update(project);
_project.Save();
}
catch (DbUpdateConcurrencyException)
{
if (!ProjectExists(model.Id))
{
return NotFound();
}
else
{
throw;
}
}
var officeIds = model.Offices.Where(x => x.Selected).Select(y => y.Value);
var existingoffices = _projectManagement.Entity.GetAll().Where(x => x.ProjectId == id).ToList();
foreach (var item in existingoffices)
{
_projectManagement.Entity.Delete(item.Id);
_projectManagement.Save();
}
foreach (var i in officeIds)
{
ProjectManagement pm = new ProjectManagement
{
OfficeId = int.Parse(i),
ProjectId = id
};
_projectManagement.Entity.Insert(pm);
_projectManagement.Save();
}
return RedirectToAction(nameof(Index));
}
return View(model);
}
Edit View:
<div class="form-group">
@for (int i = 0; i < Model.Offices.Count; i++)
{
<input type="hidden" asp-for="@Model.Offices[i].Value" />
<input asp-for="Offices[i].Selected" />@Model.Offices[i].Text
}
</div>