Marc
Marc

Reputation: 6771

How can I build a custom model binder which will return different types of models depending on the request context?

I have incoming requests (from Facebook for Credits handling) on a specific action which will have have different contents, so I have different model classes to handle that.

This is my action:

public ActionResult Index([ModelBinder(typeof(FacebookCreditModelBinder))] IFacebookRequest facebookRequest)
{
    if (facebookRequest is FacebookPaymentsGetItemsRequest)
    {
        // do whatever
    }
}

And this is my model binder.

public class FacebookCreditModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var binder = new DefaultModelBinder();
        // how to change the model here in the bindingContext?
        return binder.BindModel(controllerContext, bindingContext); 
    }
}

I want to create for example a FacebookPaymentsGetItemsRequest object if the incoming var "method" is "payments_get_items" and a FacebookPaymentsStatusUpdateRequest if method is "payments_status_update" and I don't know how to change the type of the model in the bindingContext. Is it possible to change the type of the model in a custom model binder?


Other approach: I tried it with BindModel also and I'm able to return the correct object but all properties are null because it is not filled by the default model binder:

public override object BindModel(ControllerContext controllerContext,
        ModelBindingContext bindingContext)
{
    NameValueCollection form = controllerContext.HttpContext.Request.Form;
    if (form.Get("method") == "payments_get_items")
    {
        return new FacebookPaymentsGetItemsRequest();
    }
    ...

Upvotes: 3

Views: 2471

Answers (2)

Éric Bergeron
Éric Bergeron

Reputation: 755

DefaultModelBinder seems absent in .NET Core. Other solutions on the web use the obsolete type ComplexTypeModelBinder. Some of them were not filling out not-shared fields. I spent several hours trying out stuff. So here's my simple solution.

public class FacebookCreditModelBinder : IModelBinder
{
    private readonly ModelBinderProviderContext _providerCtx;

    // pass the ModelBinderProviderContext when 
    // creating this inside your IModelBinderProvider
    public FacebookCreditModelBinder(ModelBinderProviderContext providerCtx)
    {
        _providerCtx = providerCtx;
    }

    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
            throw new ArgumentNullException(nameof(bindingContext));

        // do some code to choose type
        var chosenType = typeof(FacebookPaymentsGetItemsRequest);

        // then do this
        var metadata = _providerCtx.MetadataProvider.GetMetadataForType(chosenType);
        var binder = _providerCtx.CreateBinder(metadata, _providerCtx.BindingInfo);
        bindingContext.ModelMetadata = metadata;
        return binder.BindModelAsync(bindingContext);
    }
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could do this:

public class FacebookCreditModelBinder : DefaultModelBinder
{
    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        var methodValue = bindingContext.ValueProvider.GetValue("method");
        if (methodValue == null || string.IsNullOrEmpty(methodValue.AttemptedValue))
        {
            throw new Exception("The method parameter was not found");
        }

        var method = methodValue.AttemptedValue;
        IFacebookRequest model = null;
        if (method == "payments_get_items")
        {
            model = FacebookPaymentsGetItemsRequest();
        }
        else if (method == "...")
        {
            model = ....
        }
        else
        {
            throw new NotImplementedException("Unknown method value: " + method);
        }

        bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType());
        return model;
    }
}

and register in Application_Start:

ModelBinders.Binders.Add(typeof(IFacebookRequest), new FacebookCreditModelBinder());

Then your controller action could look like this:

public ActionResult Index(IFacebookRequest facebookRequest)
{
    if (facebookRequest is FacebookPaymentsGetItemsRequest)
    {
        // do whatever
    }
}

Upvotes: 5

Related Questions