[英]How to get FormValueProvider instance in a Custom Model Binder in ASP.NET MVC 2
I'm using the MVC 2 RC. 我正在使用MVC 2 RC。 I have a custom model binder that used to iterate over posted form values in MVC 1.0 like this: 我有一个自定义模型绑定器,用于迭代MVC 1.0中的已发布表单值,如下所示:
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. 在版本2中,我不能再这样做了。 Apparently there are new implementations of IValueProvider that come with ASP.NET MVC 2.0. 显然,ASP.NET MVC 2.0附带了IValueProvider的新实现。 I think I need to somehow use FormValueProvider in my custom model binder, but I don't know how to get at it. 我想我需要以某种方式在我的自定义模型绑定器中使用FormValueProvider,但我不知道如何实现它。
ValueProvider seems to be a read/write property. ValueProvider似乎是一个读/写属性。 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. 编辑:我不确定这个场景是否有一个很好的解决方案,但我想你最终可能会访问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. 我知道这感觉不对,但如果你的需求水平很低,我怀疑这是找到他们的唯一方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.