ASP.NET: Binding and Updating Nested Collection Objects

I made a small example with the class "Man", which contains a list of pets. In addition, there is a simple repository that can save / delete / select Person objects. Now I put the form view (or part view) in my form, select the object data source that points to my repository. As long as this works fine, I can create, update, delete people ... I can even show my collection of pets in the element template in my opinion.

But when I update a person, my collection of pets is cleared! When I look at the Update event of an object's binding source, e.InputParameters contains all my values ​​that I entered, but the pet collection always contains 0 elements. When I look at the selected object binding source event, my e.ReturnValue contains my selected person, including pets, which is correct. Any ideas?

Here is my class and my repository. This is a small example of what the linq2hibernate project actually is.

public class Person 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public int Age { get; set; } 

    public List<Pet> Pets { get; set; } 

    public Person() 
    { 
        Pets = new List<Pet>(); 
    } 
}

[DataObject] 
public class Repository 
{ 
    List<Person> people = new List<Person>(); 

    public Repository() 
    { 
         //Code to load the person list 
    } 

    public Person FindByID(int ID) 
    { 
        return people.SingleOrDefault(x => x.ID == ID); 
    } 

    public List<Person> FindAll() 
    {                         
        return people; 
    }         

    public void Save(Person person) 
    { 
       // code to save a person 
       // Here you can see a person pets collection is always empty  
       // when this method is called by the objectdatasource 
    } 

    public void Delete(Person person) 
    { 
    }        

    public void Insert(Person person) 
    { 
    } 
}
+3
source share
1 answer

, . , Save, , . Person Save. , , , /. , DetailsView , , , DetailsViewUpdatedEventArgs.Keys, NewValues ​​ OldValues.

, Pets, Person. , Save FindByID - .

, . , Person Serializable. , .

0

Source: https://habr.com/ru/post/1705421/


All Articles