asp.mvc model design

前端 未结 4 1976
鱼传尺愫
鱼传尺愫 2021-01-14 21:21

I am pretty new to MVC and I am looking for a way to design my models.

I have the MVC web site project and another class library that takes care of data access and c

4条回答
  •  猫巷女王i
    2021-01-14 21:43

    I usually use a combination of existing and custom classes for models, depending on how closely the views map to the actual classes in the datastore. For pure crud pages, using the data class as the model is perfect. In real world use though you usually end up needing a few other bits of data. I find the easiest way to combine them is something like

    // the class from the data store
    public class MyDataObject {
        public string Property1 {get;set;}
        public string Property2 {get;set;}
        }
    
    // model class in the MVC Models folder
    public class MyDataObjectModel {
        public MyDataObject MyDataObject {get;set;}
        // list of values to populate a dropdown for Property2
        public List Property2Values {get;set;}
        // related value fetched from a different data store
        public string Property3 {get;set;}
        }
    

    I also have quite a few model classes that are just a set of form fields - basically corresponding to action parameters from a particular form rather than anything that actually ends up in the database.

提交回复
热议问题