开发者

MVC 3 - Repopulate ViewModel properties with collections in Action, when ModelState is not valid

开发者 https://www.devze.com 2023-03-21 20:29 出处:网络
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.

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, th开发者_StackOverflow中文版en 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消