1

In my MVC application how can bind the foreign key/relational data in dropdownlist for the following scenario.

While showing particular course details, ALL course category should get filled and CourseCategoryID of the course should set to selected

I have created model like follow, but dropdown is filled only with the ONE value and not the entire list of course category ?

public partial class Course
{
    [Key]
    public int CourseID { get; set; }
    public int CourseCategoryID { get; set; }
    public string CourseDesciption { get; set; }
    public string CourseTitle { get; set; }

    [ForeignKey("CourseCategoryID")]
    public virtual CourseCategory CourseCategory { get; set; }
}
public partial class CourseCategory
{
    public CourseCategory()
    {
        Course = new HashSet<Course>();
    }

    [Key]
    public int CourseCategoryID { get; set; }
    public string CourseCategoryName { get; set; }
    public virtual ICollection<Course> Course { get; set; }

}

and what I need to do on View side ?

XamDev
  • 3,377
  • 12
  • 58
  • 97
  • if you display the details of one Course object, you do not need a dropdownlist. The DropdownList is required when you want to insert a new course or update and existing course , in order for user to select a coursecategory. – Lucian Bumb Jul 27 '16 at 10:05
  • @LucianBumb Exactly.. thats what I am trying to do.. Either I want to update the course OR create the new course.. So how can I proceed for that ? – XamDev Jul 27 '16 at 10:13
  • from your tags I see that you have a Asp.net5 Mvc6 project. In order to get a dropdownlist, check this question: http://stackoverflow.com/questions/34738481/mvc6-dropdownlist-of-countries – Lucian Bumb Jul 27 '16 at 10:17
  • But is there a necessity to create view model(as per provided link), if i have relationship between the models ? – XamDev Jul 27 '16 at 10:38
  • 1
    it is a good practice to send only the data which you need to the view – Lucian Bumb Jul 27 '16 at 10:42

0 Answers0