1

I have property :

public List<RequestCheckListDetail> DocumentChecklistMasterList
{
    get
    {
        if (ViewState["DocumentChecklistMasterList"].IsObjectUsable())
            _documentChecklistMasterList = (List<RequestCheckListDetail>)ViewState["DocumentChecklistMasterList"];
        else
            this._documentChecklistMasterList = new List<RequestCheckListDetail>();
        return this._documentChecklistMasterList;
    }

    set { ViewState["DocumentChecklistMasterList"] = value; }
}

I am trying to add data to it using another list. However another list has different entity, so i am running loop over first list like:

List<RequestCheckListDetail> newList = new List<RequestCheckListDetail>();

    int i = 0;
    foreach (DocumentCheckListMaster item in list)
    {

        newList.Add(new RequestCheckListDetail
        {
            Id = i,
            CheckListMaster = item
        });
        i++;
    }
    this.DocumentChecklistMasterList.AddRange(newList); 

even if newList has items in it, DocumentChecklistMasterList always have 0 items. I have tried following things:

List<RequestCheckListDetail> newList = new List<RequestCheckListDetail>();

    int i = 0;
    foreach (DocumentCheckListMaster item in list)
    {

        this.DocumentChecklistMasterList.Add(new RequestCheckListDetail
        {
            Id = i,
            CheckListMaster = item
        });
        i++;
    }


 List<RequestCheckListDetail> newList = new List<RequestCheckListDetail>();

    int i = 0;
    foreach (DocumentCheckListMaster item in list)
    {

        this.DocumentChecklistMasterList.Insert(i,
            new RequestCheckListDetail  {
            Id = i,
            CheckListMaster = item
        });
        i++;
    }

None of these codes are working properly. I am still not able to add items to DocumentChecklistMasterList

Please help me:

EDIT:

IsObjectUsable() is extension method i have added to check if object is null

 public static bool IsObjectUsable(this object checkObject)
    {
        bool isUsable = true;

        if (checkObject == null || checkObject == DBNull.Value)
        {
            isUsable = false;
        }

        return isUsable;
    }
leppie
  • 115,091
  • 17
  • 196
  • 297
user1181942
  • 1,587
  • 7
  • 35
  • 45

0 Answers0