简体   繁体   English

派生抽象类ASP.NET MVC的JSON自定义绑定程序null

[英]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. 它运作良好,但是当我将抽象类中不存在的属性添加到子类时,该属性始终为null。

Here is the code for the abstract class Pet and derived classes Dog and Cat . 这是抽象类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));
        }
    }
}

The controller just takes a Pet parameter and returns it as JSON. 控制器只接受一个Pet参数并将其作为JSON返回。

If I send 如果我发送

{name:"Odie", species:"dog"}

I get back 我回来

{"talk":"Bark!","name":"Odie","species":"dog"}

For the Cat , there is a parasite property which does not exist in the abstract class Pet . 对于Cat ,存在抽象类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. 我已经在调试器中查看了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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM