I am looking for a savior to this my problem.
I am trying to follow this tutorial about Nested Collection:
Nested collection in MVC add multiple phone number
Nevertheless, I ran into a problem with this:
internal void CreatePart(int count = 1)
{
for (int i = 0; i < count; i++)
{
Parts.Add(new Part()); // ====>>>>>> it throws null reference
}
}
I could not find the source of the null. Can you please spot it for me?
here is my codes:
entity for product
public class Product
{
public int Id { get; set; }
[StringLength(50, MinimumLength = 2)]
public string Name { get; set; }
public virtual ICollection<Part> Parts { get; set; }
public virtual ICollection<Version> Versions { get; set; }
internal void CreatePart(int count = 1)
{
for (int i = 0; i < count; i++)
{
Parts.Add(new Part());
}
}
}
entity for parts
public class Part
{
public int Id { get; set; }
[StringLength(50, MinimumLength = 2)]
public string Name { get; set; }
public int ProductId { get; set; }
public virtual Product Product { get; set; }
public string DeletePart { get; set; }
}
Create controller:
public ActionResult Create()
{
var product = new Product();
product.CreatePart(2);
return View(product);
}