开发者

Template site for MVC3; is this the proper way to display the model?

开发者 https://www.devze.com 2023-04-11 17:29 出处:网络
I\'m confused about the way Microsoft is displaying the login page in their template site. The specific phrasing for \"User name\", \"Password\", and \"Remember me?\" is set in the model itself. This

I'm confused about the way Microsoft is displaying the login page in their template site. The specific phrasing for "User name", "Password", and "Remember me?" is set in the model itself. This seems completely backwards to me. I thought that the entire point of the MVC pattern was to help with separation of concerns开发者_运维知识库; that the text displayed on the view would not be tied to the model like that. I would think you would want to just have "User name" in plain text in the view, that way it can be changed to "User ID" or translated to another language easily, without changing the model.

When creating a new page that works similarly to the login page, should I be following their template for this? Is there some advantage to having the display text defined in the model itself?


Actually what you should be passing to the view is not the model. It is a view model. View models are classes specifically tailored to meet the requirements of a given view. So it is inside this view model that you could decorate your properties with the [Display] attribute to change the text of the corresponding label or simply set it in the view when using the Html.LabelFor helper as second argument.


You can decorate viewmodel property with attibute from “System.ComponentModel.DataAnnotations” namespace and use for display label and validate model in more views and controller action. For this purpose you can use model instead viewmodel.

0

精彩评论

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