This is a continuation of a previous questions.
MVC Dropdown using ViewModels without Magic String
With the help of Stephen, I manage to make the Create methods work, now I need help with the Edit methods.
I manage to get the Get Edit () method going but when I post back it does not save the new value of the dropdown. I use the debugger and I could not see why. It looked like it was doing what it was suppose to.
// GET: Parents/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Parent parent = db.Parents.Find(id);
if (parent == null)
{
return HttpNotFound();
}
ParentVM viewModel = new ParentVM()
{
CourtList = new SelectList(db.Courts, "CourtId", "CourtName"),
SelectedCourt = parent.Court.CourtId,
ParentID = parent.ParentID,
FirstName = parent.FirstName,
LastName = parent.LastName,
Children = parent.Childs.Select(c => new ChildVM()
{
ChildID = c.ChildID,
ParentID = c.ParentID,
Name = c.Name,
DOB = c.DOB,
Address = c.Address
}).ToList(),
};
return View(viewModel);
}
// POST: Parents/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ParentVM viewModel)
{
if (ModelState.IsValid)
{
Court court = db.Courts.Find(viewModel.SelectedCourt);
var parent = new Parent()
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName,
ParentID = viewModel.ParentID,
Court = court
};
db.Entry(parent).State = EntityState.Modified;
foreach (ChildVM item in viewModel.Children)
{
var child = new Child()
{
Name = item.Name,
DOB = item.DOB,
Address = item.Address,
ParentID = viewModel.ParentID,
ChildID = item.ChildID
};
db.Entry(child).State = child.ChildID == 0 ?
EntityState.Added :
EntityState.Modified;
}
var findChild = db.Childs.Where(x => x.ParentID == viewModel.ParentID).ToList();
foreach (var item in findChild)
{
var deleteChild = viewModel.Children.Where(x => x.ChildID == item.ChildID).SingleOrDefault();
if (deleteChild == null)
{
db.Childs.Remove(item);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
viewModel.CourtList = new SelectList(db.Courts, "CourtId", "CourtName");
return View(viewModel);
}
Please try out this code. If you have any questions just ask.
Check out my comments inside the code.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ParentVM viewModel)
{
if (ModelState.IsValid)
{
Court court = db.Courts.Find(viewModel.SelectedCourt);
var parent = db.Parents.FirstOrDefault(x => x.ParentID == viewModel.ParentID);
if (parent != null)
{
// Parent exists in DB --> You can just update it
parent.FirstName = viewModel.FirstName;
parent.LastName = view;
Model.LastName;
parent.ParentID = viewModel.ParentID;
parent.Court = court;
}
else
{
// Parent does not exist in DB --> You have to Add a new Parent
db.Parents.Add(new Parent()
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName,
ParentID = viewModel.ParentID,
Court = court
});
}
foreach (ChildVM item in viewModel.Children)
{
// Here you can do exactly the same like you did bevore with your Parents
// First search for your Child in DB
var child = db.Childs.FirstOrDefault(x => x.ChildID == item.ChildID);
if (child != null)
{
child.Name = item.Name;
child.DOB = item.DOB;
child.Address = item.Address;
child.ParentID = viewModel.ParentID;
child.ChildID = item.ChildID;
}
else
{
db.Childs.Add(new Child()
{
Name = item.Name,
DOB = item.DOB,
Address = item.Address,
ParentID = viewModel.ParentID,
ChildID = item.ChildID
});
}
}
// Here I don't get what you want to do...
// Can you explain me that?
var findChild = db.Childs.Where(x => x.ParentID == viewModel.ParentID).ToList();
foreach (var item in findChild)
{
var deleteChild = viewModel.Children.Where(x => x.ChildID == item.ChildID).SingleOrDefault();
if (deleteChild == null)
{
db.Childs.Remove(item);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
viewModel.CourtList = new SelectList(db.Courts, "CourtId", "CourtName");
return View(viewModel);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.