c# – 获取父模型HttpPost更新的子集合
发布时间:2020-12-15 17:28:49 所属栏目:百科 来源:网络整理
导读:这个问题可能是对前一个问题的重复,如果是,请发布链接.无论哪种方式,我仍然会通过这篇文章. 我有这个型号: public class Employee { //omitted for brevity public virtual ICollectionProfessionalExperience ProfessionalExperiences { get; set; } publi
这个问题可能是对前一个问题的重复,如果是,请发布链接.无论哪种方式,我仍然会通过这篇文章.
我有这个型号: 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); } }
我使用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的链接. 您的问题在于,如果手动迭代并执行单独的RenderPartial()调用,则输入字段将不具有索引,并且DefaultModelBinder将不知道如何构建集合. 我个人会为你的两个ViewModel类型创建Editor Templates,并使用@ Html.EditorFor(model => model.EducationalHistories)和@ Html.EditorFor(model => model.ProfessionalExperiences). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |