Where should I create / manipulate view models in asp.net mvc?

In ASP.NET MVC, where do I need to work with view models?

Should this be done strictly in the controller? Or would it be nice to say return the view model from the repository method?

+3
source share
4 answers

In my opinion, view modes are specific to any application that will use them, while the repository will return a model common to all applications. Therefore, I would say that the presentation model should be created on the website, from the general model returned from the repository, instead of associating the repository with an understanding of how the views look.

+6
source

,

: " , , ".

(PoEAA)

+3

. ViewModel , . :

,

.

public class CustomerModel
{
    //properties
}

public class OrderModel
{
    //properties
}

public class CustomerVM
{
    public CustomerModel customer { get; set; }
    public IEnumerable<OrderModel> 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);

    }
}
+1

- - , .

0

All Articles