ASP.NET MVC 2 - 在 IValueProvider 上设置值
我正在尝试将我的 MVC 1 项目升级到 MVC 2 RC。我们目前有一个自定义模型绑定器,可以将项目添加到 ValueProvider(这在它是字典时有效)。然后我们将其传递给默认的模型绑定器。但是,IValueProvider 没有 add 方法,因此该算法不再起作用。有谁知道在 MVC 2 中向 ValueProvider 添加值的方法吗?
foreach(string valKey in controllerContext.RequestContext.HttpContext.Request.Form.AllKeys.Where(x => x.StartsWith(valuesToChangePrefix)))
{
string valName = valKey.Substring(valuesToChangePrefix.Length);
string myVal = ManipulateValue(bindingContext.ValueProvider.GetValue(valKey).AttemptedValue);
// This is where I need to add to my value Provider (As you can see I used to just assign a ValueProviderResult
//bindingContext.ValueProvider = new ValueProviderResult(myVal.Split(','), myVal, bindingContext.ValueProvider.GetValue(valKey).Culture);
}
I am attempting to upgrade my MVC 1 project to MVC 2 RC. We currently have a custom modelbinder that adds items to the ValueProvider (this worked when it was a dictionary). We then passed this off to the default modelbinder. However, IValueProvider does not have an add method, so this algorithm no longer works. Does anyone know of a way to add values to the ValueProvider in MVC 2?
foreach(string valKey in controllerContext.RequestContext.HttpContext.Request.Form.AllKeys.Where(x => x.StartsWith(valuesToChangePrefix)))
{
string valName = valKey.Substring(valuesToChangePrefix.Length);
string myVal = ManipulateValue(bindingContext.ValueProvider.GetValue(valKey).AttemptedValue);
// This is where I need to add to my value Provider (As you can see I used to just assign a ValueProviderResult
//bindingContext.ValueProvider = new ValueProviderResult(myVal.Split(','), myVal, bindingContext.ValueProvider.GetValue(valKey).Culture);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是,当您到达 ModelBinder 时,您的 ValueProvider 已经设置完毕。以前,您可以在此时向 ValueProvider 添加值。
为了提供自定义ValueProvider,您需要重写ControllerActionInvoker,这是最终的解决方案。不幸的是,ControllerActionInvoker 是由 Controller 对象创建的,而不是被注入的。因此,你还需要重写Controller来调用你自己的ControllerActionInvoker。
The problem is that by the time you get to the ModelBinder, your ValueProvider has already been set. Previously, you could add values to your ValueProvider at this point.
In order to provide a Custom ValueProvider, you need to override the ControllerActionInvoker, which is the ultimate solution. Unfortunately the ControllerActionInvoker is created by the Controller object, instead of being injected. Therefore, you also need to override Controller to call your own ControllerActionInvoker.
您不能使用 IValueProvider? (甚至创建自己的,但框架中似乎有不错的实现)
ValueProviderDictionary。 ValueProviderCollection, < a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.formcollection(VS.100).aspx" rel="nofollow noreferrer">FormCollection (和更多)他们都实现了 IValueProvider 。
我不确定是否有帮助。我不完全确定你想做什么。我不必过多地处理与帖子参数的混合。
Can't you use one of the derived types of IValueProvider? (or even create your own, but there seems to be decent implementations in the framework)
ValueProviderDictionary. ValueProviderCollection, FormCollection (and a couple more) they all implement IValueProvider.
I am not sure if it helps. I am not entirely sure of what you are trying to do. I didn't have to deal with mingling with the post params that much.
我可能误解了你的意思,但是你使用的这个前缀是什么?这与您模型的子关系有关吗?
如果不是,为什么不从
DefaultModelBinder
派生并重写BindModel()
方法?这样,您可以调用base.BindModel()
并随后操作这些值,但是您不能使用模型验证(请确保您阅读 http://bradwilson.typepad.com/blog/2010/01/ input-validation-vs-model-validation-in-aspnet-mvc.html,因为版本与 RC 相比会有重大变化)。从我猜你尝试做的事情来看,更干净的方法是在这种情况下使用 ViewModel。由于操作可能并不简单,因此您可能希望将输入和模型验证彼此分开。
不确定这是否有帮助......
I probably get you wrong, but what is this prefix you're using? Is that related to a child relation of your model?
If not, why not derive from the
DefaultModelBinder
and override theBindModel()
method? That way, you can callbase.BindModel()
and manipulate the values afterwards, however you can't use model validation then (make sure you read http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html, because there will be a significant change in the release vs. the RC).The cleaner approach, from what I can guess you try to do, is to use a ViewModel in this case. Since the manipulation is probably non-trivial, you might want to separate input and model validation from each other.
Not sure if this helps either...