0

How can I add the list of PosttypeMetas below to my viewmodel?

      static void Main(string[] args)
      {
        List<PosttypeMetas> a = new List<PosttypeMetas>();
        a.Add(new PosttypeMetas { Metatexts = "1", Selects = "mazhar" });
        a.Add(new PosttypeMetas { Metatexts = "2", Selects = "mazhar1" });
        a.Add(new PosttypeMetas { Metatexts = "3", Selects = "mazhar2" });
        a.Add(new PosttypeMetas { Metatexts = "4", Selects = "mazhar3" });
        a.Add(new PosttypeMetas { Metatexts = "5", Selects = "mazhar4" });

        PosttypeVM abc = new PosttypeVM();


        foreach (var listf in a)
        {
            PosttypeMetas abcc = new PosttypeMetas();
            abcc.Metatexts = listf.Metatexts;
            abcc.Selects = listf.Selects;

            abc.posttypemeta.Add(abcc); // NullReferenceException

        }
        Console.Read();         
    }    

public class PosttypeVM
{
    public string module { get; set; }
    public string IsModule { get; set; }
    public string parent { get; set; }    
    public string OrgorSys { get; set; }
    public string ModuleName { get; set; }
    public List<PosttypeMetas> posttypemeta { get; set; }
}

public class PosttypeMetas
{       
    public string Metatexts { get; set; }
    public string Selects { get; set; }
}
1
  • What is the error message you are getting?
    – Matt Ellen
    Commented Dec 13, 2012 at 9:51

2 Answers 2

2

The problem is that the list is a reference type and it currently refers to nothing.

You need to create the list before you can add to it. You should do this in your constructor:

public PosttypeVM()
{
    posttypemeta = new List<PosttypeMetas>();
}
1

Since a is already a List<PosttypeMetas> you dont need the foreach loop, simply assign it like this:

static void Main(string[] args)
      {
        List<PosttypeMetas> a = new List<PosttypeMetas>();
        a.Add(new PosttypeMetas { Metatexts = "1", Selects = "mazhar" });
        a.Add(new PosttypeMetas { Metatexts = "2", Selects = "mazhar1" });
        a.Add(new PosttypeMetas { Metatexts = "3", Selects = "mazhar2" });
        a.Add(new PosttypeMetas { Metatexts = "4", Selects = "mazhar3" });
        a.Add(new PosttypeMetas { Metatexts = "5", Selects = "mazhar4" });

        PosttypeVM abc = new PosttypeVM();
        abc.posttypemeta= a; //assign a to the viewmodel   

        Console.Read();         

    }    

EDIT: You will need to instantiate the list:

public class PosttypeVM
{


    public string module { get; set; }
    public string IsModule { get; set; }
    public string parent { get; set; }    
    public string OrgorSys { get; set; }
    public string ModuleName { get; set; }
    public List<PosttypeMetas> posttypemeta { get; set; }

    public PosttypeVM(){
     posttypemeta = new List<PosttypeMetas>(); //create the list 
      }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.