MVC Dropdown list bind

May 16 2018 10:53 PM
I am using code first approach
  1. [Table("tblDepartment")]  
  2. public class Department  
  3. {  
  4. [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]  
  5. public int id { getset; }  
  6. [Required(ErrorMessage = "Plz enter department name")]  
  7. public string deptName { getset; }  
  8. [Column(TypeName = "nvarchar"), MaxLength(500)]  
  9. public string deptDescriptions { getset; }  
  10. public virtual ICollection<Student> Students { getset; }  
  11. }  
  12. [Table("tblStudents")]  
  13. public class Student  
  14. {  
  15. [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]  
  16. public int stu_id { getset; }  
  17. [Required(ErrorMessage = "Plz enter Students name")]  
  18. public string stuName { getset; }  
  19. public int age { getset; }  
  20. // Foreign key  
  21. public int id { getset; }  
  22. // Navigation properties  
  23. public virtual Department Department { getset; }  
  24. }  
here the database context
  1. public DatabaseContext()  
  2. base("StudentDatabase")  
  3. {  
  4. Database.SetInitializer<DatabaseContext>(new CreateDatabaseIfNotExists<DatabaseContext>());  
  5. }  
  6. public DbSet<Department> Departments { getset; }  
  7. public DbSet<Student> Student { getset; }  
First class Departments i completed CRUD operation, but problem with the second class...
I want the department name should display in the Student view page, and when i will submit the details of a student in the database it should save only department id , but in view i want to see all the department name inside a dropdown list , form there i'll select a department and will submit....
 
i have no idea how to do that ... i am new in MVC ... can anyone help me plz???

Answers (4)