I have problem with adding/editing/deleting children list in parent object. In first method Edit(int id)
I set current parent but while I'am adding new element of children list the _currentParent
object is null
. Do you have other idea to resolve this problem ?
public class Parent
{
public int ID { get; set; }
public string Name { get; set; }
public List<Children> Childrens { get; set; }
public Parent()
{
Childrens = new List<Children>();
}
}
public class Children
{
public int ID { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
public Parent Parent { get; set; }
}
public class ParentController : Controller
{
private Parent _currentParent
...
public ViewResult Edit(int id)
{
var parent = _ParentsRepository.Find(id);
_currentParent = parent;
}
public ViewResult AddChildren(string name)
{
_currentParent.Childrens.Add(new Children(){Name = name});
}
...
}