MVC下拉式回发问题 [英] MVC dropdown postback issue
本文介绍了MVC下拉式回发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是以前的问题的延续。
This is a continuation of a previous questions.
在斯蒂芬的帮助下,我设法使创建方法工作,现在我需要帮助编辑方法。
With the help of Stephen, I manage to make the Create methods work, now I need help with the Edit methods.
我设法获取Get Edit()方法,但是当我发回它不会保存下拉列表的新值。我使用调试器,我看不到为什么。它看起来像是在做什么想法。
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);
}
这篇关于MVC下拉式回发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文