這是我的自定義模型綁定器,用於實例化派生類。自定義多態模型綁定器不綁定派生類型的屬性
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
上的屬性。爲什麼是這樣?