这个问题可能是对前一个问题的重复,如果是,请发布链接.无论哪种方式,我仍然会通过这篇文章.
我有这个型号:
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); } }
Here is my View:
06002
我使用foreach在视图上显示子集合,并使用每个集合的局部视图.
在调用Post Edit Action时,员工模型将子集合设置为null.
[HttpPost] public ActionResult Edit(Employee employee) { using(var context = new EPMSContext()) { } return View(); }
为了正确地收集儿童藏品,我错过了什么?
谢谢!
解决方法
我认为问题与MVC期望构建集合元素的方式有关(它们在html中的名称).
看看这个SO答案: https://stackoverflow.com/a/6212877/1373170,特别是与Scott Hanselman的 post的链接.
看看这个SO答案: https://stackoverflow.com/a/6212877/1373170,特别是与Scott Hanselman的 post的链接.
您的问题在于,如果手动迭代并执行单独的RenderPartial()调用,则输入字段将不具有索引,并且DefaultModelBinder将不知道如何构建集合.
我个人会为你的两个viewmodel类型创建Editor Templates,并使用@ Html.EditorFor(model => model.EducationalHistories)和@ Html.EditorFor(model => model.ProfessionalExperiences).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。