RobbertS RobbertS - 2 months ago 16
ASP.NET (C#) Question

Setting data in multiple nested lists ASP.NET MVC C#

I'm an intern with very basic knowledge of ASP and C#.
I'm trying to display a list of projects > maps > thememaps in an application I'm working on in ASP.NET MVC. In my third foreach I get an error saying: "Does not contain a definition for "ThemeMaps" and no extension method "ThemeMaps" accepting a first argument of type could be found".

I'm confused as to why vmProject.Maps does not contain the property ThemeMaps. I instantiated that list just like maps. What am I doing wrong?

LayersController.cs

// Create viewmodel object
var viewModel = new AddLayerToThemeMapViewModel();

// Create Project list
viewModel.Projects = new List<AddProject>();

// Loop over all maps
List<Project> projects = this.applicationDb.Projects.OrderBy(e => e.Title).ToList();
foreach (var project in projects)
{
// Create map
var vmProject = new AddProject()
{
ProjectId = project.ProjectID,
ProjectTitle = project.Title,
Maps = new List<AddLayerMap>(),
};

foreach (var map in project.Maps.OrderBy(e => e.Title))
{
// Create map
vmProject.Maps.Add(new AddLayerMap()
{
MapId = map.MapId,
MapTitle = map.Title,
ThemeMaps = new List<AddLayerMapThemeMap>(),
});

// Loop over all thememaps in map
foreach (var thememap in map.ThemeMaps.OrderBy(e => e.Order))
{
vmProject.Maps.ThemeMaps.Add(new AddLayerMapThemeMap()
{
ThemeMapId = thememap.ThemeMapId,
ThemeMapTitle = thememap.Title,
});
}
}

// Add map to list
viewModel.Projects.Add(vmProject);
}


My viewmodel class

using Mapgear.MapViewer.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Mapgear.MapViewer.ViewModels
{
public class AddLayerToThemeMapViewModel
{
public Guid LayerId { get; set; }

public List<AddProject> Projects { get; set; }
}

public class AddProject
{
public Guid ProjectId { get; set; }

public string ProjectTitle { get; set; }

public List<AddLayerMap> Maps { get; set; }
}

public class AddLayerMap
{
public Guid MapId { get; set; }

public string MapTitle { get; set; }

public List<AddLayerMapThemeMap> ThemeMaps { get; set; }
}

public class AddLayerMapThemeMap
{
public Guid ThemeMapId { get; set; }

public string ThemeMapTitle { get; set; }
}
}


I made a scetch before I started on paper, which looks like the following:


  • LayerId

  • List project


    • ProjectId

    • ProjectTitle

    • List Map


      • MapId

      • MapTitle

      • List ThemeMap


        • ThemeMapId

        • ThemeMapTitle






I know my class names are a bit out of wack, however I din't write them myself. Gonna optimize them after.

PS: This is my first question on StackOverflow!

Answer

I modified the code in the second for-each loop a bit. I added a variable for the newly created map and add the thememaps to this created map. Check if it's correct.

var newMap = new AddLayerMap()
{
    MapId = map.MapId,
    MapTitle = map.Title,
    ThemeMaps = new List<AddLayerMapThemeMap>(),
};
// Create map
vmProject.Maps.Add(newMap);

// Loop over all thememaps in map
foreach (var thememap in map.ThemeMaps.OrderBy(e => e.Order))
{
    newMap.ThemeMaps.Add(new AddLayerMapThemeMap()
    {
        ThemeMapId = thememap.ThemeMapId,
        ThemeMapTitle = thememap.Title,
    });
}