【问题标题】:How to add a list of objects inside a list in C# MVC如何在 C# MVC 中的列表中添加对象列表
【发布时间】:2018-11-30 08:36:17
【问题描述】:

我有一个包含如下部分列表的视图模型。我需要创建一个 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; }
    }
}

【问题讨论】:

  • 到目前为止你尝试过什么?能否再具体说明一下你的问题,好像有点不清楚。
  • 您不需要在 SubSectionModel 和 ResponseEntryViewModel 类上添加构造器
  • 你应该避免在类中创建类。你必须知道如何使用new 来创建类的新对象并使用Add 方法将对象添加到列表中。您尝试过使用它们吗?
  • 是的,我试过@ChetanRanpariya
  • 你尝试的时候发生了什么?你能分享那个代码吗?你有什么错误吗?什么错误?

标签: c# asp.net-mvc list


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多