我浏览了该网站上的其他问题和答案,但找不到我需要的答案。
我有一个 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; }
}
我正在将学生记录传递给包含在视图模型中的视图:
public class UserViewModel {
public StudentRecord Student;
public ICurrentUserService CurrentUserService;
public ParentUser ParentUser;
}
然后在表单中显示它以便可以对其进行编辑,并提交表单将 StudentRecord 传递回控制器。除了 StudentRecord 中的地址为空外,一切正常。如果添加了新地址,StudentRecord 中的单个 StudentAddress 也可以正常工作。
是否可以编辑地址并将其发送回控制器,或者我是否需要将它们以单独的形式放在单独的页面上?我可以做到这一点,但更愿意将它们合二为一。
我的问题可能是不可能,或者可能是我将地址放入表单的方式。一个学生可能有多个地址。
这是表格:(为了清楚起见,我删除了一些 html 布局。“添加另一个地址”复选框显示带有 jquery 的新学生地址部分。)
@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*@