ASP.NET MVC 2 – 绑定到抽象模型

如果我有以下强类型视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 

位置是抽象类。

我有以下控制器,它通过POST接受强类型模型

 [HttpPost] public ActionResult Index(Location model) 

我收到一个运行时错误,指出“无法创建抽象类

这当然有道理。 但是 – 我不确定这里最好的解决方案是什么。

我有很多具体的类型(大约8个),这是一个只能编辑抽象类属性的视图。

试图做的是为所有不同的具体类型创建重载,并以通用方法执行我的逻辑。

 [HttpPost] public ActionResult Index(City model) { UpdateLocationModel(model); return View(model); } [HttpPost] public ActionResult Index(State model) { UpdateLocationModel(model); return View(model); } 

等等

然后:

 [NonAction] private void UpdateLocationModel (Location model) { // ..snip - update model } 

但这也不起作用 ,MVC抱怨动作方法含糊不清(也很有意义)。

我们做什么? 我们可以简单地绑定到抽象模型吗?

如何为这个抽象类编写自定义模型绑定器:

 public class CustomBinder : DefaultModelBinder { protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) { // TODO: based on some request parameter choose the proper child type // to instantiate here return new Child(); } } 

只有当您有一个基于某些用户操作动态插入输入元素的表单时,这才有意义。 在这种情况下,您需要传递一些额外的参数来指示您需要哪个具体类。 否则我会坚持使用具体的视图模型作为动作参数。

您还可以构建适用于所有抽象模型的通用ModelBinder。 我的解决方案要求您在视图中添加一个名为“ ModelTypeName ”的隐藏字段,并将值设置为您想要的具体类型的名称。 但是,应该可以使这个更聪明,并通过将类型属性与视图中的字段匹配来选择具体类型。

Application_Start()中的 Global.asax.cs文件中:

 ModelBinders.Binders.DefaultBinder = new CustomModelBinder(); 

CustomModelBinder:

 public class CustomModelBinder2 : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var modelType = bindingContext.ModelType; if (modelType.IsAbstract) { var modelTypeValue = controllerContext.Controller.ValueProvider.GetValue("ModelTypeName"); if (modelTypeValue == null) throw new Exception("View does not contain ModelTypeName"); var modelTypeName = modelTypeValue.AttemptedValue; var type = modelType.Assembly.GetTypes().SingleOrDefault(x => x.IsSubclassOf(modelType) && x.Name == modelTypeName); if (type != null) { var instance= bindingContext.Model ?? base.CreateModel(controllerContext, bindingContext, type); bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instance, type); } } return base.BindModel(controllerContext, bindingContext); } } 

只是把它扔出去 – 我对其他人可能回答的问题非常感兴趣,但这就是我在遇到类似情况时最终做的事情;

基本上,我没有使用模型类作为Action方法中的参数,而是传入FormCollection并测试一些已知的鉴别器以确定要创建/编辑的类型,然后从那里使用TryUpdateModel

似乎可能有更好的方式,但我从来没有想过要更多。