Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends,
I have below model structure
C#
public class CollectionGroupViewModel
{
        public int TenantID { get; set; }

        public string Market { get; set; }

        public string GroupName { get; set; }

        public string GTS { get; set; }

        public string ProcessingCenter { get; set; }

}

public class EditCollectionGroupViewModel : CollectionGroupViewModel
{
        public IList<SelectListItem> ProcessingCenter { get; set; }
}

I have class to create Mock data as follows:
XML
public static List<CollectionGroupViewModel> GetCollectionGroupViewList()
        {
            List<CollectionGroupViewModel> modelList = new List<CollectionGroupViewModel>();

            for (int i = 0; i < 10; i++)
            {
                CollectionGroupViewModel model = new CollectionGroupViewModel();

                model.TenantID = i + 1000;
                model.Market = "UK";
                model.GroupName = "Collection Group 1";
                model.GTS = "yes";
                model.ProcessingCenter = "Processing Centre 1";

                modelList.Add(model);
            }

            return modelList;
        }

        public static CollectionGroupViewModel GetEditCollectionGroupViewModel(int tenantID)
        {
            List<CollectionGroupViewModel> modelList = GetCollectionGroupViewList();

            EditCollectionGroupViewModel model = modelList.Where(x => x.TenantID == tenantID).Single();
            model.ProcessingCenter = ?;
            return model;
        }

What I want is, I want to copy the following properties from the parent class to the child class.
TenantID, Market, GroupName, GTS

And I want to edit the following property before sending the result.
model.ProcessingCenter = ?;

Any idea how to do it?

Thanks in advance
Posted

1 solution

Um...the child class EditCollectionGroupViewModel derives from the parent CollectionGroupViewModel class - so it already contains all the properties for the parent.

You don't need to copy values, unless you are trying to create a new child based on the parent. In that case, I'd create a child constructor which accepted a parent as the parameter and copied the values.

C#
public EditCollectionGroupViewModel(CollectionGroupViewModel parent)
   {
   TenantID = parent.TenantID; 
   ...
   }
But a better way would be to create an instance of teh child class in teh first place instead of the parent:
C#
for (int i = 0; i < 10; i++)
{
    CollectionGroupViewModel model = new EditCollectionGroupViewModel();

    model.TenantID = i + 1000;
    model.Market = "UK";
    model.GroupName = "Collection Group 1";
    model.GTS = "yes";
    model.ProcessingCenter = "Processing Centre 1";

    modelList.Add(model);
}
 
Share this answer
 
Comments
dhage.prashant01 21-Oct-14 8:07am    
I tried the same..!!
Bingo..
dhage.prashant01 21-Oct-14 8:08am    
I have created the constructor in child class

public class EditCollectionGroupViewModel : CollectionGroupViewModel
{
public SelectlistView ProcessingCenterList { get; set; }

public EditCollectionGroupViewModel(CollectionGroupViewModel model)
{
this.TenantID = model.TenantID;
this.Market = model.Market;
this.GroupName = model.GroupName;
this.GTS = model.GTS;
this.ProcessingCenter = model.ProcessingCenter;
this.ProcessingCenterList = null;
}
}

An this is how I passed the parent object to the constructor
var model = GetCollectionGroupViewList().Where(x => x.TenantID == tenantID).Select(x => new EditCollectionGroupViewModel(x)).Single();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900