I am using code first approach
- [Table("tblDepartment")]
- public class Department
- {
- [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]
- public int id { get; set; }
- [Required(ErrorMessage = "Plz enter department name")]
- public string deptName { get; set; }
- [Column(TypeName = "nvarchar"), MaxLength(500)]
- public string deptDescriptions { get; set; }
- public virtual ICollection<Student> Students { get; set; }
- }
- [Table("tblStudents")]
- public class Student
- {
- [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]
- public int stu_id { get; set; }
- [Required(ErrorMessage = "Plz enter Students name")]
- public string stuName { get; set; }
- public int age { get; set; }
-
- public int id { get; set; }
-
- public virtual Department Department { get; set; }
- }
here the database context
- public DatabaseContext()
- : base("StudentDatabase")
- {
- Database.SetInitializer<DatabaseContext>(new CreateDatabaseIfNotExists<DatabaseContext>());
- }
- public DbSet<Department> Departments { get; set; }
- public DbSet<Student> Student { get; set; }
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???