"创建"观看亲子关系MVC 4

时间:2016-07-28 10:33:56

标签: asp.net-mvc asp.net-mvc-4 parent-child

所以我有这个模型:

 public class Subthing
 {
     public virtual int SubthingId { get; set; }
     public virtual int ThingId { get; set; }

     public virtual string Name { get; set; }

     public virtual int ParentId { get; set; }
     public virtual Subthing Parent { get; set; }
     public virtual List<Subthing> Childs { get; set; }

     public virtual Thing Thing{ get; set; }
}

我想创建一个创建视图来介绍与子主题相关的子项。我有这个控制器:

public ActionResult CriarMoreSubthings(int id)
        {

            var x = db.Subthings.FirstOrDefault(e => e.SubthingId == id);

            if (x == null)
            {
                return HttpNotFound();
            }

            var subthing = new Subthing()
            {
                ThingId = x.ThingId,
                ParentId = id //(also tried x.SubthingId)
            };

            return View("CriarMoreSubthings", subthing);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CriarMoreSubthings(Subthing subthing)
        {
            if (ModelState.IsValid)
            {
                db.Subthings.Add(subthing);
                db.SaveChanges();              
                return RedirectToAction(blabla);
            }

            return View(subthing);
        }

但它始终使用parentId = 0创建,但parentId应该是subthing id。 (为了记录,它可能都是错的,我已经搜索了很多关于这个,但我不能使它工作,新手在这里)。我究竟做错了什么?感谢

0 个答案:

没有答案