繁体   English   中英

如何在C#MVC中的列表内添加对象列表

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

我有一个包含以下部分列表的视图模型。 我需要创建一个ResponseEntryViewModel列表,并在各节内添加各节和子节以及各分节内的问题。

有什么建议么?

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; }
    }
}

尝试这个:

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; }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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