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"