从自定义模型绑定器的controllerContext中获取FormCollection
我有一个很好的函数来获取我的 FormCollection(从控制器提供)。现在我想做一个模型绑定,并让我的模型绑定器调用该函数,它需要 FormCollection。由于某种原因我能找到它。我以为会是这样 controllerContext.HttpContext.Request.Form
I had a nice function that took my FormCollection (provided from the controller). Now I want to do a model bind instead and have my model binder call that function and it needs the FormCollection. For some reason I can find it. I thought it would have beencontrollerContext.HttpContext.Request.Form
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
FormCollection 是我们添加到 ASP.NET MVC 中的一种类型,它有自己的 ModelBinder。您可以查看 FormCollectionBinderAttribute 的代码来了解我的意思。
Try this:
FormCollection is a type we added to ASP.NET MVC that has its own ModelBinder. You can look at the code for FormCollectionBinderAttribute to see what I mean.
直接访问表单集合似乎是不受欢迎的。以下是来自 MVC4 项目的示例,其中我有一个自定义 Razor EditorTemplate,它在单独的表单字段中捕获日期和时间。自定义绑定器检索各个字段的值并将它们组合成一个
DateTime
。Accessing the form collection directly appears to be frowned on. The following is an example from an MVC4 project where I have a custom Razor EditorTemplate that captures the date and time in separate form fields. The custom binder retrieves the values of the individual fields and combines them into a
DateTime
.使用bingdingContext.ValueProvider(和bingdingContext.ValueProvider.TryGetValue等)直接获取值。
Use bindingContext.ValueProvider (and bindingContext.ValueProvider.TryGetValue, etc.) to get values directly.