简体   繁体   中英

How to add a list of objects inside a list in C# MVC

I have a view model containing list of sections like below. I need to create a list of ResponseEntryViewModel and add sections and sub sections inside sections and questions inside subsections.

Any suggestions?

public class ResponseEntryViewModel
{
    public int TypeID { get; set; }
    public string TypeName { get; set; }
    public int User_ID { get; set; }
    public List<SectionDataModel> Sections{ get; set; }

    public ResponseEntryViewModel()
    {
        Sections = new List<SectionDataModel>();           
    }

    public class SectionDataModel
    {
        public int SectionID { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
        public int TypeId { get; set; }
        public List<SubSectionModel> SubSections { get; set; }
        public SectionDataModel()
        {
            SubSections = new List<SubSectionModel>();
        }
    }

    public class SubSectionModel
    {
        public int SubSectionID { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
        public int SectionId { get; set; }
        public List<QuestionModel> QuestionsList { get; set; }
        public SubSectionModel()
        {

            QuestionsList = new List<QuestionModel>();
        }
    }

    public class QuestionModel
    {
        public int SubSectionID { get; set; }
        public int QuestionID { get; set; }
        public string Question { get; set; }
    }
}

Try this:

public class ResponseEntryViewModel
{
    public int TypeID { get; set; }
    public string TypeName { get; set; }
    public int User_ID { get; set; }
    public List<SectionDataModel> Sections { get; set; }

    public ResponseEntryViewModel(SectionDataModel obj)
    {
        Sections = new List<SectionDataModel>();
        Sections.Add(obj);
    }

    public class SectionDataModel
    {
        public int SectionID { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
        public int TypeId { get; set; }
        public List<SubSectionModel> SubSections { get; set; }
        public SectionDataModel(SubSectionModel obj)
        {
            SubSections = new List<SubSectionModel>();
            SubSections.Add(obj);
        }
    }

    public class SubSectionModel
    {
        public int SubSectionID { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
        public int SectionId { get; set; }
        public List<QuestionModel> QuestionsList { get; set; }

        public SubSectionModel(QuestionModel obj)
        {
            QuestionsList = new List<QuestionModel>();
            QuestionsList.Add(obj);
        }
    }

    public class QuestionModel
    {
        public int SubSectionID { get; set; }
        public int QuestionID { get; set; }
        public string Question { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM