此問題可能是對上一個問題的重申,如果是請發佈鏈接。無論哪種方式,我仍然會通過這篇文章。爲父模型獲取子集合HttpPost更新
我有這樣的模式:
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對象中? –