Defining view models for MVC / MVP

后端 未结 3 1926
陌清茗
陌清茗 2021-01-17 00:18

Short question - how do you define your view models?

Here are some of the options:

  1. Pass the actual model into the view.
  2. Create a view model wi
3条回答
  •  既然无缘
    2021-01-17 01:09

    In my projects, it's a mix really.

    If I want to display a form with details of Customer X, I just pass a DAL Customer object to my view. It's really no use to create a seperate ViewModel for it, map all its properties, and then display them. It's a waste of time imho.

    Sometimes though, models are a bit more complex. They're the result of multiple queries, have some added data to them, so in these cases, I create a custom ViewModel, and add the necessary data from my model to it. In your case, it would be option 2, or sometimes 3. I prefer that over passing my model and having to add an additional 10 items in my ViewData.

提交回复
热议问题