我浏览了这个网站上的其他问题和答案,但找不到我需要的答案。
我有一个studentrecord实体:

public class StudentRecord : Persistent {
        public virtual string LastName { get; set; }
        public virtual string FirstName { get; set; }
        public virtual DateTime Dob { get; set; }
        public virtual int StudentRef { get; set; }
        public virtual IEnumerable<StudentAddress> Addresses { get; set; }
        public virtual StudentAddress Address { get; set; }
        public virtual string Notes { get; set; }
    }

如您所见,它包含一个studentaddress实体和一个studentaddress的IEnumerable:
public class StudentAddress: Persistent {
        public virtual int StudentRef { get; set; }
        public virtual string Addressee { get; set; }
        public virtual string Property { get; set; }
        public virtual string District { get; set; }
        public virtual string PostalTown { get; set; }
        public virtual string County { get; set; }
        public virtual string Postcode { get; set; }
    }

我正在将学生记录传递到包含在ViewModel中的视图:
public class UserViewModel {
        public StudentRecord Student;
        public ICurrentUserService CurrentUserService;
        public ParentUser ParentUser;
    }

然后将其显示在表单中以便进行编辑,提交表单会将studentrecord传递回控制器。除了studentrecord中的地址为空外,其他都可以正常工作。studentrecord中的单个studentaddress用于添加新地址,这也可以正常工作。
是否可以编辑地址并将其发送回控制器,或者我需要将它们以单独的形式放在单独的页面上?我能做到,但我更希望这一切都在一个。
我的问题可能是这是不可能的,也可能是我把地址写进表格的方式。学生可以有多个地址。
这是表单:(为了清晰起见,我去掉了一些html布局。“add another address”复选框显示了带有jquery的new student address部分。)
@using (Html.BeginForm()) {
    Personal Details
    Full Name: @Html.TextBoxFor(x => x.Student.FirstName) @Html.TextBoxFor(x => x.Student.LastName)
    DOB: @Html.TextBoxFor(x => x.Student.Dob)

    @if (Model.Student.Addresses.Any()) {
        // Only print addresses if they exist
            int count = 1;
            int element = 0;
                @if (Model.Student.Addresses.Count() > 1) {
                    foreach (var address in Model.Student.Addresses) {
                        Student Address @count
                        Addressee @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Addressee)
                        Property @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Property)
                        District @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).District)
                        Postal Town @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).PostalTown)
                        County @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).County)
                        Postcode @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Postcode)
                        count++;
                        element++;
                    } //end foreach
                } else {
                    Student Address
                    Addressee @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Addressee)
                    Property @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Property)
                    District @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).District)
                    Postal Town @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).PostalTown)
                    County @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).County)
                    Postcode @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Postcode)
                } @*end if (Model.Student.Addresses.Count() > 1)*@

                Add another address @Html.CheckBox("Add another address", false, new {@id = "newBox"})

                New Student Address
                Addressee @Html.TextBoxFor(x => x.Student.Address.Addressee)
                Property @Html.TextBoxFor(x => x.Student.Address.Property)
                District @Html.TextBoxFor(x => x.Student.Address.District)
                Postal Town @Html.TextBoxFor(x => x.Student.Address.PostalTown)
                County @Html.TextBoxFor(x => x.Student.Address.County)
                Postcode @Html.TextBoxFor(x => x.Student.Address.Postcode)
    } else {
        No address for this student.
    } @*end if (Model.Student.Addresses.Any())*@

    Notes: @Html.TextAreaFor(x => x.Student.Notes, new { @style = "width: 100%;"})

    <input type="submit" value="Send" class="btn btn-primary" style="clear: both;"/>
} @*end of form*@

最佳答案

问题是文本输入控件的name属性不包含正确的值。我邀请您阅读following blog post以更好地理解默认模型绑定器用于绑定到集合和字典的约定。
那么我建议您使用编辑器模板,而不是在视图中编写foreach循环:

@using (Html.BeginForm()) {
    Personal Details

    @Html.LabelFor(x => x.Student.FirstName, "Full Name:")
    @Html.EditorFor(x => x.Student.FirstName)
    @Html.EditorFor(x => x.Student.LastName)

    @Html.LabelFor(x => x.Student.Dob, "DOB:")
    @Html.TextBoxFor(x => x.Student.Dob)

    @if (Model.Student.Addresses.Any()) {
        @Html.EditorFor(x => x.Student.Addresses)
    } else {
        <text>No address for this student.</text>
    }

    @Html.LabelFor(x => x.Student.Notes, "Notes:")
    @Html.TextAreaFor(x => x.Student.Notes, new { @style = "width: 100%;"})

    <input type="submit" value="Send" class="btn btn-primary" style="clear: both;"/>
}

然后定义一个自定义编辑器模板,该模板将为地址集合(~/Views/Shared/EditorTemplates/StudentAddress.cshtml)的每个元素自动呈现:
@model StudentAddress
@Html.LabelFor(x => x.Addressee, "Addressee")
@Html.EditorFor(x => x.Addressee)

@Html.LabelFor(x => x.Property, "Property")
@Html.EditorFor(x => x.Property)

@Html.LabelFor(x => x.District, "District")
@Html.EditorFor(x => x.District)

@Html.LabelFor(x => x.PostalTown, "Postal Town")
@Html.EditorFor(x => x.PostalTown)

@Html.LabelFor(x => x.County, "County")
@Html.EditorFor(x => x.County)

@Html.LabelFor(x => x.Postcode, "Postcode")
@Html.EditorFor(x => x.Postcode)

但这一切都是静止的。如果你想动态地添加和删除地址,我邀请你阅读史蒂文·桑德森的following blog post,他在其中演示了如何使用自定义html助手为输入字段(Html.BeginCollectionItem)生成正确的名称,并使用ajax添加新行。

08-20 02:37