Attaching validation to EF objects used in MVC controllers/views?

前端 未结 3 1180
青春惊慌失措
青春惊慌失措 2021-01-21 04:26

We\'re throwing together a quick project (CRUD forms) and decided to skip view models and use EF entities directly in controllers and views. Since I\'m not used to this approach

3条回答
  •  一个人的身影
    2021-01-21 04:48

    Easiest thing to do is to use the DataAnnotations attributes that are in the System.ComponentModel.DataAnnotations anmespace.

    MVC respects those and will populate your ModelError collection if any fail. In the case of your example, you could add a using statement for that namespace and then just flag a property with

    [StringLength(25)]
    

    and call it a day.

提交回复
热议问题