views:

217

answers:

1

Hi,

I wish to use a modelbinder I've made directly on an action method parameter. Such as:

public ActionResult MyAction([ModelBinder(typeof(MyBinder))] string param1)

However, I need to pass a string into the binder itself, so I was wondering if you could do something along the lines of:

public ActionResult MyAction([MyBinder("mystring")] string param1)

Is it possible?

A: 

No, you cannot pass parameters via the attribute declaration. If you look at the source code for ModelBinderAttribute, you will see that its constructor takes only a type argument, that it has no other properties, and that it is a sealed class. So this road is a dead end.

The only way to get information into the ModelBinder that I know of is the FormCollection itself.

You could, however, make a parent binder type and subtype it for each param value which you intend to use. It's messy, but it would work in the example you give.

Craig Stuntz
Okay, thank you for your answer
Sir Code-A-Lot
you can get url and query string parameters on POST not just the form collection as you have access to the httpcontext within the model binder
monkeylee
@monkeylee the modelbinder doesn't provide you a FormCollection but you can just create a new one from Request.Form. FormCollection is just a wrapper. See answer from @haacked here http://stackoverflow.com/questions/1510946/get-formcollection-out-controllercontext-for-custom-model-binder
Simon_Weaver