0

I'm new to c sharp. I"ve got an ASP.NET MVC Core 2.0 project and I need to populate a list inside a model and return that model to the page. My model looks like this:

public class PageModel
     public string Title {get; set;}
     public List<ChartGroups> Chart { get; set; }    

}

public class ChartGroups
{
    public string Freq { get; set; }    
    public string Head { get; set; }
}

To populate this I've got the following:

public PageModel GetChart(){
 PageModel R = new PageModel(); 

 R.Title = "Some Title";
 R.Chart.Add(New ChartGroups {Freq ="Test", Head="Test2"});
 R.Chart.Add(New ChartGroups {Freq ="Test3", Head="Test4"});
 return(R);
}

The problem is I get an error saying I need to obstinate the object when I get to the R.Chart.Add... line. I've been doing this with drop down list and it works just fine....I'm sure I'm missing something simple just can't see it.

UPDATE: I know that I need to obstinate the object, just not sure how to do so under this context.

jps
  • 20,041
  • 15
  • 75
  • 79
Chris
  • 265
  • 2
  • 16
  • You cannot call `Add()` to a un-instantiated List variable. In your `PageModel` constructor you should initialize the property: `Chart = new List();` – maccettura Feb 16 '18 at 16:27
  • It means you have to initialize `R.Chart` to an instance before you can access it's members. Your method could also be simplified to one return statement: `public PageModel GetChart() { return new PageModel { Title = "Some Title", Chart = new List { new ChartGroups { Freq = "Test", Head = "Test2" }, new ChartGroups { Freq = "Test3", Head = "Test4" } } };` – Rufus L Feb 16 '18 at 16:44
  • mm8 and tjugg both are great answers and I've upvoted both. If I could I would mark both as the answer. Thanks! – Chris Feb 16 '18 at 16:46

3 Answers3

2

You need to create an instance of the List<ChartGroups>() before you can start adding items to it. You can either do this in your GetChart() method:

public PageModel GetChart()
{
    PageModel R = new PageModel();

    R.Title = "Some Title";
    R.Chart = new List<ChartGroups>(); //< ---
    R.Chart.Add(New ChartGroups { Freq = "Test", Head = "Test2"});
    R.Chart.Add(New ChartGroups { Freq = "Test3", Head = "Test4"});
    return R;
}

Or in your PageModel class:

public class PageModel
{
    public string Title { get; set; }
    public List<ChartGroups> Chart { get; set; } = new List<ChartGroups>();
}
mm8
  • 163,881
  • 10
  • 57
  • 88
0

You need to Initialize the List. So for example, in your PageModel constructor you can do:

public class PageModel  
{
    public PageModel()
    {
        Chart = new List<ChartGroups>();
    }

    public string Title {get; set;}
    public List<ChartGroups> Chart { get; set; } 
}

Just to inform, you don't probably need the { set; } part of the Chart property since you are now initializing the property in the constructor, so you can just put it as

 public List<ChartGroups> Chart { get; } 

And then, if you wish, you can also you syntax like

 public List<ChartGroups> Chart { get; } = new List<ChartGroups>(); 

Which would allow you to move the initialization out of the constructor.

tjugg
  • 2,977
  • 2
  • 18
  • 24
-1

Your list is null. So set it to an instance:

public List<ChartGroups> Chart { get; set; } = new List<ChartGroups>()
Steve Harris
  • 5,014
  • 1
  • 10
  • 25