MVC视图模型inheritance和创建动作

我正在尝试找出在MVC应用程序中处理模型类型层次结构的最佳架构。

鉴于以下假设模型 –

public abstract class Person { public string Name { get; set; } } public class Teacher : Person { public string Department { get; set; } } public class Student : Person { public int Year { get; set; } } 

我可以为每种类型创建一个控制器。 对于使用显示模板的视图,人员将只有索引和细节操作,教师和学生将只有创建/编辑操作。 这会起作用但看起来很浪费,并且不会真正扩展,因为如果在层次结构中添加了另一种类型,则需要新的控制器和视图。

有没有办法在Person控制器中进行更通用的创建/编辑操作? 我已经搜索了一段时间的答案,但似乎无法找到我正在寻找的确切,所以任何帮助或指针将不胜感激:)

当然,但需要一点腿工作。

首先,在每个编辑/创建视图中,您需要发出正在编辑的模型类型。

其次,您需要为person类添加新的模型绑定器。 以下是我为此做的原因示例:

 public class PersonModelBinder :DefaultModelBinder { protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) { PersonType personType = GetValue(bindingContext, "PersonType"); Type model = Person.SelectFor(personType); Person instance = (Person)base.CreateModel(controllerContext, bindingContext, model); bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instance, model); return instance; } private T GetValue(ModelBindingContext bindingContext, string key) { ValueProviderResult valueResult =bindingContext.ValueProvider.GetValue(key); bindingContext.ModelState.SetModelValue(key, valueResult); return (T)valueResult.ConvertTo(typeof(T)); } } 

在您的应用开始注册:

 ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder()); 

PersonType是我倾向于在每个模型中使用的,并且是一个枚举,说明每种类型是什么,我在HiddenFor中发出它,以便它返回到post数据。

SelectFor是一个返回指定枚举类型的方法

 public static Type SelectFor(PersonType type) { switch (type) { case PersonType.Student: return typeof(Student); case PersonType.Teacher: return typeof(Teacher); default: throw new Exception(); } } 

您现在可以在控制器中执行类似的操作

 public ActionResult Save(Person model) { // you have a teacher or student in here, save approriately } 

Ef能够通过TPT样式inheritance非常有效地处理这个问题

只是为了完成这个例子:

 public enum PersonType { Teacher, Student } public class Person { public PersonType PersonType {get;set;} } public class Teacher : Person { public Teacher() { PersonType = PersonType.Teacher; } }