Html.BeginCollectionItem只有嵌套属性为null

时间:2013-05-15 17:35:43

标签: asp.net-mvc-4 null nested

我使用Html.BeginCollectionItem帮助程序获取已编辑的变量长度列表。它适用于非嵌套属性(字符串Member.Info,int Member.MotherID等),但所有嵌套属性为空(母会员。母亲等)......

会员班:

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

Controller snippet:

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

Index.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在该Controller中检索它吗?

0 个答案:

没有答案