这是我的自定义模型绑定器,用于实例化派生类。
public class LocationModalBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext,
Type modelType)
{
var type = bindingContext.ModelName + "." + "type";
Type typeToInstantiate;
switch ((string) bindingContext.ValueProvider.GetValue(type).RawValue)
{
case "store":
{
typeToInstantiate = typeof (Store);
break;
}
case "billing":
{
typeToInstantiate = typeof(LocationReference);
break;
}
case "alternate":
{
typeToInstantiate = typeof(Address);
break;
}
default:
{
throw new Exception("Unknown location identifier.");
}
}
return base.CreateModel(controllerContext, bindingContext, typeToInstantiate);
}
}
问题是它没有绑定子类型的属性。只有基本类型的属性Location
。为什么是这样?