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!
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
):