Saturday, 7 September 2013

Updating child objects when editing parent

Updating child objects when editing parent

I'm struggling to update a related entity in my Edit Post action.
I have a Job and a JobNotes model that look like this:
public class Job
{
public Job()
{
JobNotes = new List<JobNote>();
}
[Key]
public int JobID { get; set; }
public string jobName { get; set; }
public ICollection<JobNote> JobNotes { get; set; }
}
public class JobNote
{
[Key]
public int JobNoteID { get; set; }
public string Author { get; set; }
public string Note { get; set; }
public Job Job { get; set; }
}
I've also used Fluent API to map my relationships:
(Any feedback as to whether I've done this correctly is most welcome!)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Job>()
.HasMany(x => x.JobNotes)
.WithOptional(y => y.Job);
}
And the problem is that in my Post method my JobNotes object is being
added to the parent Job object, but not being saved to the database.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(JobViewModel model)
{
if (ModelState.IsValid)
{
var existingJob = db.Jobs
.Include(x => x.JobNotes)
.Single(c => c.JobID == model.Job.JobID);
model.Job.JobNotes.Add(new JobNote
{
Author = "System",
Note = "Job modified by " + User.Identity.Name
});
db.Entry(existingJob).CurrentValues.SetValues(model.Job);
db.SaveChanges();
return RedirectToAction("Details", new { id = model.Job.JobID });
}
}
What have I done wrong? Thanks in advance for your help.

No comments:

Post a Comment