3

我使用 Html.BeginCollectionItem 帮助器来获取已编辑的可变长度列表。它适用于非嵌套属性(字符串 Member.Info、int Member.MotherID 等),但所有嵌套属性均为空(Mother Member.Mother 等)...

会员等级:

 public class Member : Person
 {
    [Key] 
    public int MemberID { get; set; }
    ...
    public string Info { get; set; }

    public int MotherID { get; set; }

    public virtual Mother Mother { get; set; }
 }

嵌套母类:

 public class Mother : Person
 {
    [Key]
    public int MotherID { get; set; }

    public virtual ICollection<Member> Children { get; set; }

    public Mother()
    {
        Children = new List<Member>();
    }
}

控制器片段:

 [HttpPost]
 public ActionResult GoSend(IEnumerable<Member> me) // Mother in 'me' is null
 {...}

索引.cshtml:

 @model IEnumerable<Directory_MVC.Models.Member>
 ...
 @using (Html.BeginForm("GoSend", "Members", FormMethod.Post))
 {
    @foreach (var item in Model)
    {
        Html.RenderPartial("EditorRow", item);       
    }
 }

EditorRow.cshtml:

 @model Directory_MVC.Models.Member
 ...
 @using (Html.BeginCollectionItem("me"))
 {
      @Html.HiddenFor(x => x.Info)
      @Html.HiddenFor(x => x.MotherID)
      @Html.HiddenFor(x => x.Mother)
      @Html.HiddenFor(x => x.Mother.Email)
 }

为损坏的母亲输入生成的 HTML 是:

 <input id="me_88c3c30f-3aa7-4729-9879-368f01073080__Mother" name="me[88c3c30f-3aa7-4729-9879-368f01073080].Mother" type="hidden" value="System.Data.Entity.DynamicProxies.Mother_5D771F511FE55BBA8F7C7D21A0054616D2EB9278EDCA1D59DFDCB1B68EA45920" />

为工作 MotherID 输入生成的 HTML 是:

 <input data-val="true" data-val-number="The field MotherID must be a number." data-val-required="The MotherID field is required." id="me_88c3c30f-3aa7-4729-9879-368f01073080__MotherID" name="me[88c3c30f-3aa7-4729-9879-368f01073080].MotherID" type="hidden" value="1" />

我究竟做错了什么?是否甚至可以序列化该 Mother 对象并通过 Html.BeginCollectionItem 在该控制器中检索它?

4

0 回答 0