JSON custom binder null for derived abstract class asp.net mvc
I have made a custom binder for an abstract class. The binder decides which implementation to use. It works well, but when I add a property which does not exist in the abstract class to a child class, it is always null.
Here is the code for the abstract class Pet
and derived classes Dog
and Cat
.
public abstract class Pet
{
public string name { get; set; }
public string species { get; set; }
abstract public string talk { get; }
}
public class Dog : Pet
{
override public string talk { get { return "Bark!"; } }
}
public class Cat : Pet
{
override public string talk { get { return "Miaow."; } }
public string parasite { get;set; }
}
public class DefaultPetBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Type modelType)
{
bool hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);
string prefix = ((hasPrefix)&&(bindingContext.ModelName!="")) ? bindingContext.ModelName + "." : "";
// get the parameter species
ValueProviderResult result;
result = bindingContext.ValueProvider.GetValue(prefix+"species");
if (result.AttemptedValue.Equals("cat")){
//var model = base.CreateModel(controllerContext, bindingContext, typeof(Cat));
return base.CreateModel(controllerContext,bindingContext,typeof(Cat));
}
else (result.AttemptedValue.Equals("dog"))
{
return base.CreateModel(controllerContext,bindingContext,typeof(Dog));
}
}
}
The controller just takes a Pet
parameter and returns it as JSON.
If I send
{name:"Odie", species:"dog"}
I get back
{"talk":"Bark!","name":"Odie","species":"dog"}开发者_StackOverflow中文版
For the Cat
, there is a parasite property which does not exist in the abstract class Pet
. If I send
{"parasite":"cockroaches","name":"Oggy","species":"cat"}
I get back
{"talk":"Miaow.","parasite":null,"name":"Oggy","species":"cat"}
I have tried this with other more complex classes, this is just a simple example.
I have looked in the debugger, the parasite
value is in the value provider, the model the binder returns contains a field for the parasite.
Can anyone see where the problem is?
Try like this:
protected override object CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Type modelType)
{
bool hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);
string prefix = ((hasPrefix)&&(bindingContext.ModelName!="")) ? bindingContext.ModelName + "." : "";
// get the parameter species
ValueProviderResult result;
result = bindingContext.ValueProvider.GetValue(prefix+"species");
if (result.AttemptedValue.Equals("cat"))
{
var model = Activator.CreateInstance(typeof(Cat));
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(Cat));
return model;
}
else if (result.AttemptedValue.Equals("dog"))
{
var model = Activator.CreateInstance(typeof(Dog));
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(Dog));
return model;
}
throw new Exception(string.Format("Unknown type \"{0}\"", result.AttemptedValue));
}
精彩评论