Defining view models for MVC / MVP

后端 未结 3 1939
陌清茗
陌清茗 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 00:55

    I grabbed the T4 templates from SubSonic 3. These were modified and I added some new ones. I can run one of them and it generates 3 separate view models for each table. Then I can modify as needed.

    Why three?

    1. FormModel - contains on the data necessary for displaying in a form for editing or creation. Foreign keys get converted to SelectLists. DateTime fields get split into date and time components.

    2. PostModel - this is the object returned from the Form Post. DropDownLists are posted as Int or equivalent type. Only the necessary members are in the model.

    3. DisplayModel - used for non-editing display of the data.

    I always generated these in a subfolder named Generated. As I hand tweek them I move them to the Models folder. It doesn't completely automate the process, but it generates a lot of code I would otherwise generate by hand.

提交回复
热议问题