【发布时间】:2012-09-08 01:23:28
【问题描述】:
这个问题可能是对上一个问题的重复,如果是,请发布链接。无论哪种方式,我仍然会完成这篇文章。
我有这个模型:
public class Employee {
//omitted for brevity
public virtual ICollection<ProfessionalExperience> ProfessionalExperiences { get; set; }
public virtual ICollection<EducationalHistory> EducationalHistories { get; set; }
}
public class ProfessionalExperience {
// omitted for brevity
}
public class EducationalHistory {
// omitted for brevity
}
我正在使用此操作在我的视图上显示:
[HttpGet]
public ActionResult Edit(int id) {
using(var context = new EPMSContext()) {
var employees = context.Employees.Include("ProfessionalExperiences").Include("EducationalHistories");
var employee = (from item in employees
where item.EmployeeId == id && item.IsDeleted == false
select item).FirstOrDefault();
return View(employee);
}
}
这是我的观点:
@using(Html.BeginForm()) { <div class="editor-label">First Name:</div> <div class="editor-field">@Html.TextBoxFor(x => x.FirstName)</div> <div class="editor-label">Middle Name:</div> <div class="editor-field">@Html.TextBoxFor(x => x.MiddleName)</div> @foreach(var item in Model.ProfessionalExperiences) { Html.RenderPartial("ProfExpPartial", item); } @foreach(var item in Model.EducationalHistories) { Html.RenderPartial("EducHistPartial", item); } <input type="submit" value="Save" /> }
我使用foreach 在视图上显示子集合,并为每个集合使用部分视图。
当调用我的 Post Edit Action 时,employee 模型将子集合设置为 null。
[HttpPost]
public ActionResult Edit(Employee employee) {
using(var context = new EPMSContext()) {
}
return View();
}
为了让子集合正确,我缺少什么?
谢谢!
【问题讨论】:
-
您能说明您使用什么代码来发起 POST 请求吗?还显示一个示例 HTTP POST 请求也会有所帮助,即您要发布什么数据?听起来 ASP.NET 无法反序列化集合,因此它有助于查看您尝试发送的数据。
-
我已经更新了我的问题,希望您的建议正确。
-
另外,我检查了 Request.Form 对象,发现集合在请求中。如何让我的子集合包含在我传递的 Employee 对象中?
标签: c# asp.net-mvc razor