C# 无法创建MVC子级

C# 无法创建MVC子级,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我是MVC新手,所以请原谅我的问题。我有一个Person对象/类和一个子对象/类 public partial class Person { public Person() { this.Registrations = new HashSet<Registration>(); this.Children = new HashSet<Child>(); }

我是MVC新手,所以请原谅我的问题。我有一个Person对象/类和一个子对象/类

  public partial class Person
    {
        public Person()
        {
            this.Registrations = new HashSet<Registration>();
            this.Children = new HashSet<Child>();
        }

        public int PKey { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public int StateKey { get; set; }
        public string ZipCode { get; set; }
        public string Phone { get; set; }

        public virtual State State { get; set; }
        public virtual ICollection<Registration> Registrations { get; set; }
        public virtual ICollection<Child> Children { get; set; }

 public partial class Child
    {
        public int PKey { get; set; }
        public int ParentKey { get; set; }
        public string Name { get; set; }
        public System.DateTime BirthDate { get; set; }

        public virtual Person Person { get; set; }
    }
我想我完全错过了某个地方的船。任何帮助都将不胜感激。谢谢


BJ

关键部分首先是此处的属性名称
new{id=

@Html.ActionLink("Add Child to Registration", "AddChild", new { id = Model.PKey })
它应该与GET方法中的参数匹配,并且可以通过子模型传递:

[HttpGet]
public ActionResult AddChild(int? id)
{       
    return View(new Child{ ParentKey = id });
}
通过填充子模型的此属性,并将其传递给
视图(
),可以在AddChild.cshtml视图中使用此属性。在表单中,表单正文中可能会声明一个隐藏字段,以便在保存时发布此值:

Html.HiddenFor(m=>m.ParentKey);

ActionLink
将指向一个
[HttpGet]AddChild
方法,因此该方法上应该有一个
int-id
参数。但是,您只显示了[HttpPost]方法。应该有一个
[HttpGet]AddChild
方法。是://GET:Children Create public-ActionResult-Create(int?id){return View();}我不确定在这里放什么来设置ParentKey
[HttpGet]
public ActionResult AddChild(int? id)
{       
    return View(new Child{ ParentKey = id });
}
Html.HiddenFor(m=>m.ParentKey);