Where should viewmodels be created/manipulated in asp.net mvc?

To my mind viewmodels are specific to whatever application is going to use them, whereas a repository would return a model common to all applications. So I'd say the view model should be created within the web site, from a common model returned from the repository, rather than tie the repository into knowing about how views are laid out.

Strictly speaking your repository should return domain objects Repository: "Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. " Fowler (PoEAA).

Viewmodels should be instantiated in your controller and passed to the strongly-typed view. Typically a ViewModel will contain multiple model types necessary to render your view. Here's a Quick Example: Say you have two model objects Orders and Customer You want to display Customer Details at the top of the page and a Grid of Orders in the Index view for that customer.

Public class CustomerModel { //properties } public class OrderModel { //properties } public class CustomerVM { public CustomerModel customer { get; set; } public IEnumerable orders { get; set; } } //and in your controller public class CustomerController : Controller { public ActionResult Index(int id) { CustomerVM vm = new CustomerVM(); vm. Customer = CustomerRepository. GetCustomer(id); vm.

Orders = OrdersRepository. GetOrdersForCustomer(id); return View(vm); } }.

Repository should be a in between your domain and UI - the repository should know nothing about your UI layer - so you can get the best re-use and decoupling from it as possible.

Viewmodels should created in (.viewmodels should created at (.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions