我在本网站上查看了其他问题和答案,但找不到我需要的答案。
我有一个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实体以及一个IEnumerable of StudentAddress:
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布局。&#39;添加另一个地址&#39;复选框显示带有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*@
答案 0 :(得分:8)
问题是文本输入控件的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)
但这一切都是静态的。如果您希望能够动态添加和删除地址,我邀请您阅读Steven Sanderson的following blog post,其中说明了如何使用自定义HTML帮助程序为输入字段生成专有名称({{1并使用AJAX添加新行。