在搜索 Stack Overflow 和 Google 时,我无法找到明确的答案,希望有人能指出我正确的方向。
我的情况 我希望能够使用单个编辑表单(在单个视图中)使用 ASP.NET MVC 3 和 Entity Framework 4 CTP(代码优先)更新 3 级深层次实体 - 该模型由服务,可以有许多服务选项,而服务选项又可以有许多库存项目。
我期待能够使用 MVC 默认模型绑定器(通过 TryUpdateModel)来:
- 更新现有的“服务”记录
- 根据发布的值添加/更新/删除“服务选项”记录(附加到服务)
- 根据发布的值添加/更新/删除“库存”记录(附加到每个服务选项)
我的模特
[Bind(Include="Name, ServiceOptions")]
public class Service {
[Key]
public int ServiceID { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; set; }
public virtual ICollection<ServiceOption> ServiceOptions { get; set; }
}
[Bind(Include="ServiceOptionID, Description, Tags")]
public class ServiceOption {
[Key]
public int ServiceOptionID { get; set; }
public int ServiceID { get; set; } /* parent id reference */
public string Description { get; set; }
public virtual ICollection<Inventory> InventoryItems { get; set; }
}
[Bind(Include = "InventoryID, Description")]
public class Inventory {
[Key]
public int InventoryID { get; set; }
public int ServiceOptionID { get; set; } /* parent id reference */
public string Description { get; set; }
}
理想控制器方法:
[HttpPost]
public ActionResult EditService(int id) {
Service service = db.Services.Single(s => s.ServiceID == id);
TryUpdateModel(service); // automatically updates child and grandchild records
if (ModelState.IsValid) {
db.SaveChanges();
return RedirectToAction("Index");
}
return View(service);
}
有没有办法实现这个乌托邦式的梦想,还是我找错了树?我愿意使用另一种技术(例如普通的 EF4、Automapper 等)
提前致谢!