我何时使用视图模型、部分、模板并通过 MVC 3 处理子绑定
mvc3 新手,我有几个问题,如果有人可以回答/提供链接,我将不胜感激:
- 何时应该使用视图模型?不建议使用域名吗?我发现我的视图模型是域对象的副本,并且看不到值...
- 我什么时候应该使用 Partials?难道只有部分视图才会被重用吗?
- 我什么时候应该使用显示模板和编辑器模板?我可以在没有视图模型的情况下使用它们吗?
- 如何创建父对象和子对象列表均可编辑的编辑屏幕?即顶部的几个字段(父级)和下面的字段网格(如可编辑行),特别是,我如何进行绑定?不使用自动映射器。
谢谢!
new to mvc3, i have a few questions, would appreciate if someone could answer/provide links:
- When should I use View Models? Is it not recommended to use the domain? I find that my view models are copies of my domain objects, and don't see the value...
- When should I use Partials? Is it only if the partial view will be reused?
- When should I use Display Templates and Editor Templates? Can I use these without a view model?
- How do I create an edit screen where the parent and list of child objects are both editable? i.e. a few fields at the top (parent) and a grid of fields below (like editable rows), particularly, how do i do the binding? automapper is not used.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应始终使用视图模型。您不应在视图中使用域模型。
视图模型不是域模型的精确副本。它们总是存在一些与视图的具体要求相关的差异。例如,您希望在一个屏幕上显示域模型的一些属性,而在另一屏幕上显示其他属性。因此,您还将有不同的验证要求,因为一个屏幕上需要一个属性,而其他屏幕上则不需要。因此,这些视图模型上也会有不同的数据注释。
不仅视图会被重用。部分可用于使您的视图更加结构化。另外,如果您使用 AJAX,部分功能会更容易。您可以将 AJAX 请求发送到控制器操作,该操作将返回部分视图,允许您仅更新 DOM 的部分内容。
总是。您可以将它们与任何强类型模型一起使用,但您应该始终使用视图模型(请参阅上一个问题的答案)。
这是一个相当广泛的问题,但要回答它,一如既往,您首先要定义视图模型,该视图模型将表示/包含您想要在此屏幕上显示以进行编辑的属性:
然后是控制器:
最后是视图:
最后一部分是子项的编辑器模板 (
~/Views/Home/EditorTemplates/ChildViewModel.cshtml
):View models should always be used. You should not use your domain models in the view.
View models are not exact copies of the domain models. They always have some differences related to the specific requirements of the view. For example on one screen you would like to present some of the properties of your domain model and on other screen other properties. As a consequence to this you will also have different validation requirements as one property will be required on one screen and not required on other screen. So you will also have different data annotations on those view models.
Not only if the view will be reused. Partials could be used to make your views more structured. Also if you are using AJAX, partials make it easier. You would send the AJAX request to a controller action which will return a partial view allowing you to update only portions of the DOM.
Always. You can use them with any strongly typed model, but you should always use a view models (see answer to previous question).
That's a pretty broad question but to answer it as always you start with defining your view models which will represent/contain the properties you would like to present on this screen for editing:
then a controller:
and finally the view:
and the last piece is the editor template for a child (
~/Views/Home/EditorTemplates/ChildViewModel.cshtml
):