Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on MVC 5, there is a viewmodel class and contain a virtual list as the same class.

Issue occurred, the problem is filled list is now empty. please help me sir...

I did write:- PartnerProjectVM partnerProjectVM = new PartnerProjectVM();

with out using this new key word how can I access my list? mean, 'partnerProjectVM.listPartnerProjectVM' like this

What I have tried:

Viewmodel,

C#
public class PartnerProjectVM  
   {  
       public int idPartnerProject { get; set; }  
       public string ProjectName { get; set; }  
       public string ProjectDescription { get; set; }  
      public virtual List<PartnerProjectVM> listPartnerProjectVM { get; set; }  
   }  


Get/Post Method,

C#
[HttpPost]  
        public ActionResult PremiumUserRegProjects(PartnerProjectVM partnerProjectVM  
        {  
             partnerProjectVM.listPartnerProjectVM = new List<PartnerProjectVM>();  
                partnerProjectVM.listPartnerProjectVM.Add(partnerProjectVM);  
                return RedirectToAction("PremiumUserRegProjects");  
        }  
          
         [HttpGet]  
        public ActionResult PremiumUserRegProjects()  
        {  
             PartnerProjectVM partnerProjectVM = new PartnerProjectVM();  
             if (partnerProjectVM.listPartnerProjectVM != null && partnerProjectVM.listPartnerProjectVM.Count() > 0)  
            {  
              var lastItemProject = partnerProjectVM.listPartnerProjectVM.LastOrDefault();  
  
                partnerProjectVM.ProjectName = lastItemProject.ProjectName;  
                partnerProjectVM.ProjectDescription = lastItemProject.ProjectDescription;  
                
  
                return View("PremiumUserRegProjects", partnerProjectVM);  
            }  
        }  
Posted
Updated 12-Mar-17 23:04pm

1 solution

Give the class a constructor and set the variable there

public class PartnerProjectVM  
   {  
       public int idPartnerProject { get; set; }  
       public string ProjectName { get; set; }  
       public string ProjectDescription { get; set; }  
      public virtual List<PartnerProjectVM> listPartnerProjectVM { get; set; }  

    public PartnerProjectVM ()
    {
        this.listPartnerProjectVM  = new List<PartnerProjectVM>()
    }
   } 
 
Share this answer
 
Comments
Kats2512 13-Mar-17 7:58am    
thought as much that the list was not being included in the public constructor. +5

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