MVC下拉回发问题

时间:2014-12-22 04:48:15

标签: c# asp.net-mvc entity-framework

这是以前问题的延续。

MVC Dropdown using ViewModels without Magic String

在Stephen的帮助下,我设法使Create方法起作用,现在我需要有关Edit方法的帮助。

我设法让Get Edit()方法继续,但是当我回发它时,它不会保存下拉列表的新值。我使用调试器,我不明白为什么。看起来它正在做它想象的事情。

// 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);
    }

1 个答案:

答案 0 :(得分:0)

请试用此代码。如果你有问题,就问吧。
查看我在代码中的评论。

[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);
}