tags:

views:

30

answers:

1

I'm not sure if this is possible but I want to see objects created by my ModelBinders other than having them passed as parameters to my Action methods.

I.e. I want to register a FooBinder and a BarBinder, then look at a Foo in the following method

public void MyAction(Bar bar)

or even ideally in an ActionFilter.

Is this possible?

+2  A: 

To access this:

 public ActionResult FizzAction(object foo) // <--
    {...}

Use this in your filter:

public class BarFilter : ActionFilterAttribute
{
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     var foo = filterContext.ActionParameters["foo"];
     //do whatever you want with it
 }
}

EDIT:

For ActionMethodSelectorAttribute this might help:

 public class foo : ActionMethodSelectorAttribute
    {
        public override bool IsValidForRequest
            (ControllerContext controllerContext, MethodInfo methodInfo)
        {
            ValueProviderResult valueResult;
            controllerContext.Controller.ValueProvider  
                .TryGetValue("foo", out valueResult);
        }
    }

Check out this blog post by K. Scott Allen.

No warranty - haven't used this by myself - just found through watch window. :)

Arnis L.
Thanks Arnis. That's absolutley the right answer. However I've just realised it's actually not an ActionFilter I want to access it in but an ActionMethodSelectorAttribute. I'm guessing this isn't possible...?
Gaz
one moment.. .
Arnis L.
Edited my post. I hope it helps.
Arnis L.
Nice answer Arnis +1. Please, check after `ValueProviderResult valueResult` should be ';' or '=' ?
eu-ge-ne
There is ; and i think everything is fine with that. Thanks.
Arnis L.
Thanks for the attempt Arnis but it looks like the ValueProvider is just a decoupled way of accessing values from the Request. The results of the ModelBinders are not in there.I'm guessing that the complete set of bound objects may not be ready by this point as the decision of which action to call could influence the decision about which model binders to use.
Gaz
Sorry, I'm stupid - its `out valueResult` there. My apologies :)
eu-ge-ne
@Gaz, I was worrying about something like that but couldn't define it so neat as you just did. Seems that now i'm clueless. :/
Arnis L.