How to fix 'non-nullable property' warning in ASP.NET Razor Pages application?

370 Views Asked by At

Warning CS8618 Non-nullable property 'Categories_List' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. RazorPageDemoYoutube D:\ASP.NET\RazorPage\RazorPageDemo\RazorPageDemoYoutube\Pages\Admin\Categories\Index.cshtml.cs 16 Active

enter image description here

2

There are 2 best solutions below

1
Qing Guo On

Try to add ? ,like :

public IEnumerable<Category>? Categories_List { get; set; }

OR:

public IEnumerable<Category> Categories_List { get; set; } = new List<Category>();
0
Michael Harvey On

Initialize the property to the default.

public IEnumerable<Category> Categories_List { get; set; } = default!;