简体   繁体   English

如何在ASP.NET MVC 2中的自定义模型绑定器中获取FormValueProvider实例

[英]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.

相关问题 用于GET请求的ASP.NET MVC的自定义模型Binder - Custom Model Binder for ASP.NET MVC on GET request 在ASP.NET MVC中使用自定义模型绑定程序验证整数字段 - Validating integer field with custom model binder in ASP.NET MVC 如何让ASP.NET Core 2.0 MVC Model Binder绑定没有FromBody属性的复杂类型 - How to get the ASP.NET Core 2.0 MVC Model Binder to bind complex types without FromBody attributes 具有继承关系的模型的asp.net mvc默认模型绑定程序 - asp.net mvc default model binder for model that has inheritance asp.net Core 2自定义模型Binder,具有复杂的模型 - asp.net Core 2 Custom Model Binder with complex model ASP.Net MVC模型绑定程序-忽略属性 - ASP.Net MVC Model Binder - Ignore Properties 用于抽象模型的Asp.Net Core 2.0(MVC)Content Binder - Asp.Net Core 2.0 (MVC) Content Binder for an abstract model asp.net mvc: 默认model binder问题 - asp.net mvc: default model binder problem 在 ASP.NET MVC 中的自定义模型绑定器中绑定属于另一个对象的列表 - Binding a list belonging to another object in a custom model binder in ASP.NET MVC 如何在模型创建期间覆盖ASP.NET MVC 3默认模型绑定器以解析依赖关系(使用ninject)? - How to override the ASP.NET MVC 3 default model binder to resolve dependencies (using ninject) during model creation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM