JSON自定义绑定器null用于派生抽象类asp.net mvc

sem*_*maz 1 c# defaultmodelbinder model-binding asp.net-mvc-3

我为抽象类创建了一个自定义绑定器.绑定器决定使用哪种实现.它工作得很好,但是当我将一个在抽象类中不存在的属性添加到子类时,它总是为null.

下面是抽象类的代码Pet和派生类DogCat.

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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器只接受一个Pet参数并将其作为JSON返回.

如果我发送

{name:"Odie", species:"dog"}
Run Code Online (Sandbox Code Playgroud)

我回来了

{"talk":"Bark!","name":"Odie","species":"dog"}
Run Code Online (Sandbox Code Playgroud)

对于Cat,有一个寄生属性,在抽象类中不存在Pet.如果我发送

{"parasite":"cockroaches","name":"Oggy","species":"cat"}
Run Code Online (Sandbox Code Playgroud)

我回来了

{"talk":"Miaow.","parasite":null,"name":"Oggy","species":"cat"}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过其他更复杂的类,这只是一个简单的例子.我查看了调试器,parasite值在值提供程序中,binder返回的模型包含寄生虫的字段.任何人都可以看到问题出在哪里?

Dar*_*rov 5

试试这样:

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));
}
Run Code Online (Sandbox Code Playgroud)