'Why we need to constructor inside a model in ASP.NET MVC using C#, and what is the purpose of this?

public class TagVM
{
    public string TagName { get; set; }
}

public class TagListVM
{
    public List<TagVM> TagList { get; set; }

    public TagListVM()
    {
        TagList = new List<TagVM>();
    }
}

Why do we use TagListVM constructor and what is purpose of it?



Solution 1:[1]

Without the constructor TagList will be null (unless you initialize it elsewhere).

for more refer this Model with constructor vs. Model without constructor ASP.NET MVC

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammad Asad Fazlani