Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have 3 tables subject , topic and sub topic. topic has subjectID as foreign key while subtopic has topic id as foreign key. I am using EF6 DB First to generate model classes.

I have created the Create view for subject which works fine. Now i want to create a create view for Topic. after scaffolding it created the view for me and i can see the dropdownlist code generated there for subjects. when when i run it i get error
"
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'SubjectId
"

So i get that since i am not passing anything to the get method , it can't populate the dropdown. My question is how do i do so.'

this is my subject class

public partial class Subject
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Subject()
        {
            this.Topics = new HashSet<Topic>();
        }
    
        public int Id { get; set; }
        public string SubjectName { get; set; }
        public string LogoPath { get; set; }
        public string LogoName { get; set; }
        public Nullable<System.DateTime> CreatedDate { get; set; }
        public Nullable<System.DateTime> ModifiedDate { get; set; }
        public Nullable<bool> IsActive { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Topic> Topics { get; set; }
    }


this is topic class

public partial class Topic
   {
       [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
       public Topic()
       {
           this.SubTopics = new HashSet<SubTopic>();
       }

       public int Id { get; set; }
       public Nullable<int> SubjectId { get; set; }
       public string TopicName { get; set; }
       public string TopicLogoPath { get; set; }
       public string TopicLogoName { get; set; }
       public string Description { get; set; }
       public Nullable<System.DateTime> CreatedDate { get; set; }
       public Nullable<System.DateTime> ModifiedDate { get; set; }
       public Nullable<bool> IsActive { get; set; }

       public virtual Subject Subject { get; set; }
       [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
       public virtual ICollection<SubTopic> SubTopics { get; set; }
   }



What is the correct way to pupulate subject dropdown in Topic and Topic dropdown in subtopic.
Any sort help is much appreciated.

Thanks

What I have tried:

I tried creating a ViewModel Class with an IEnumerable<subject> feild. I then populated this feild in a get method which i then pass to the view.

public class TopicVM
   {
       public int Id { get; set; }
       public Nullable<int> SubjectId { get; set; }
       public string TopicName { get; set; }
       public string TopicLogoPath { get; set; }
       public string TopicLogoName { get; set; }
       public string Description { get; set; }
       public Nullable<System.DateTime> CreatedDate { get; set; }
       public Nullable<System.DateTime> ModifiedDate { get; set; }
       public Nullable<bool> IsActive { get; set; }

       public IEnumerable<Subject> Subjects { get; set; }

   }


this is the method that populates the property

public TopicVM GetList()
        {
            TopicVM topicVM = new TopicVM();
            topicVM.Subjects = _entities.Subjects.Where(x => x.IsActive == true).Select(x=>x).ToList();
            return topicVM;
        }


Now when i am trying to add view using TopicVM as the model class i recieve error that "Unable to retreive metadata for QR.ViewModels.TopicVM"
Posted
Updated 23-Nov-17 8:24am
v2

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