MVC 3 - 当 ModelState 无效时,使用 Action 中的集合重新填充 ViewModel 属性
我有一个 ViewModel,它具有多个属性,其中包含用于填充表单中的 DropDowns 的集合。在get Action中,填充这些属性,然后正确渲染视图。
当用户提交表单时,在 Post 操作中,我的 ViewModel 包含填写表单的所有数据,但所有集合属性均为空,因为浏览器不发送它们。没关系,当 ModelState 有效时,我就不再需要它们了。但是,当 ModelState 无效时,我需要再次显示表单以更正错误。
MVC 是否将这些集合临时存储在任何地方,ViewModel 属性可以轻松地填充与 get 操作中相同的值,还是我必须手动填充它们(从缓存或新查询到数据库)?
我认为,MVC 存储原始值,因此可以在后期操作的验证过程中使用它们,但我无法理解。
谢谢
I have a ViewModel with several properties containing Collections for populating the DropDowns in form. In get Action, these properties are filled, and then view is properly rendered.
When user sumbits form, then in Post action my ViewModel contains all data filled in form, but all collection properies are null, because browser doesn't send them. That's ok, when ModelState is valid, so i don't need them anymore. But, when the ModelState is not valid I need to show the form again for correcting the errors.
Is MVC storing these collections temporary anywhere, co the ViewModel properties can be easily filled with same values as in get action, or do I have to populate them manually (from cache or new query to db) ?
I thought, that MVC stores original values, so they can be used during validation in post action, but I can't figure it.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能可以使用自定义 ModelBinder
查看 吉米·博加德。它并不完全是您所需要的,但它可以给您一些想法。
You could probably use a custom ModelBinder
Check out this post by Jimmy Bogard. It isn't exactly what you need, but it can give you some ideas.