简体   繁体   中英

How to get FormValueProvider instance in a Custom Model Binder in ASP.NET MVC 2

I'm using the MVC 2 RC. I have a custom model binder that used to iterate over posted form values in MVC 1.0 like this:

var values = bindingContext.ValueProvider.Where(x => x.Key.StartsWith("prefix"));    
foreach (var value in values) { /* do something */ }

In version 2, I can't do this anymore. Apparently there are new implementations of IValueProvider that come with ASP.NET MVC 2.0. I think I need to somehow use FormValueProvider in my custom model binder, but I don't know how to get at it.

ValueProvider seems to be a read/write property. Have you tried this:

var formValueProvider = new FormValueProvider(controllerContext);
bindingContext.ValueProvider = formValueProvider;

I discussed this on another question as well but to be honest I haven't used it myself.

Edit: I am not sure if there is a good solution for this scenario, but I guess you might end up accessing controllerContext.HttpContext.Request. I know it doesn't feel right but if your needs are so low level I suspect that's the only way to get to them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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