开发者

ASP .NET MVC 2 - ViewModels vs regular Views and Models

开发者 https://www.devze.com 2023-01-16 07:26 出处:网络
I\'m a bit new to ASP .NET MVC and I\'m having trouble understanding the need for ViewModels, and wha开发者_运维百科t makes them different from regular Models. Any good resources on the topic ? They a

I'm a bit new to ASP .NET MVC and I'm having trouble understanding the need for ViewModels, and wha开发者_运维百科t makes them different from regular Models. Any good resources on the topic ?


They are strongly tied to the view. For example your Model might contain many properties but you only need to manipulate a subset of them on a given view. In this case you would create a ViewModel to represent the properties you would like to work with on a given view. So you could have multiple ViewModels for a given Model because this Model might have many representations. A ViewModel might also contain formatted data which is more suitable to be shown on a view, while the Model contains raw data (for example DateTime formatting, currencies, etc...).

Everytime you write a single line of C#/VB.NET code in a view it means that this is a good candidate for a view model and/or html helper.

0

精彩评论

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