Neil Thompson
Neil Thompson

Reputation: 6425

Custom model binder: how to determine sub class

I have a custom model binder on a controllers POST action:

public ActionResult Detail(IProduct model)

Detail is actually passed a FooProduct, which inherits from Product: IProduct

In the model binders BindModel() method I have

The bindingContext.ModelType is IProduct.

When I inspect the ControllerContext or BindingContext I can't find anything that lets me know if IProduct's concrete type is a FooProduct or a BarProduct.

How can I find this out?

I can work around this problem, but I'm curious as to how it should be resolved properly.

Upvotes: 0

Views: 56

Answers (1)

SebastianStehle
SebastianStehle

Reputation: 2459

You have only 2 options:

1) Add an additional fields (query-string, form-fields...) to your request that identifies the type of the model.

2) Inspect all fields and try to figure out which subclass you need for the specified values.

The binder itself has no information other than the request and the requested type (IProduct), so you have to make your decision based on this input.

Upvotes: 1

Related Questions