如果我有以下强类型视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<XXX.DomainModel.Core.Locations.Location>" %>
哪里 位置 是一个抽象类。
我有以下控制器,它通过一个接受强类型模型 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'将值设置为您想要的具体类型的名称。
但是,应该可以使这个更聪明,并通过将类型属性与视图中的字段匹配来选择具体类型。
在你的 的Global.asax.cs 档案 的Application_Start():
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
从那里。
似乎可能有更好的方式,但我从来没有想过要更多。