Kyle Oliver

Kyle Oliver

  • 1.8k
  • 65
  • 5.1k

How to get two dropdown lists on same page

Feb 17 2020 8:07 AM
Hi,
 
I have an Employee Class in which you have to capture the employee's physical and postal address.
Both the physical and postal address require a Province.
I have created a Province Class to store the Provinces.
 
I need to have a dropdown list of the Provinces for both the physical and postal address of a single employee but when I add the second dropdown list, it doesn't work.
 
My Employee Class is as follows;
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.ComponentModel.DataAnnotations.Schema;  
  5. using System.Linq;  
  6. using System.Web;  
  7.   
  8. namespace LMKSystem.Models  
  9. {  
  10.     public class Employee  
  11.     {  
  12.         [Key]  
  13.         public int EmployeeId { getset; }  
  14.   
  15.         [Display(Name = "Employee Number")]  
  16.         [MaxLength(50)]  
  17.         public string EmployeeNumber { getset; }  
  18.   
  19.         //Foreign Key from Title Class  
  20.         [Display(Name = "Title")]  
  21.         public int TitleId { getset; }  
  22.   
  23.         [ForeignKey("TitleId")]  
  24.         public virtual Title Titles { getset; }  
  25.         //end  
  26.   
  27.         [MaxLength(50)]  
  28.         //[Required(ErrorMessage ="An employee name is needed to continue")]  
  29.         public string Name { getset; }  
  30.   
  31.         [Display(Name = "Second Name")]  
  32.         [MaxLength(50)]  
  33.   
  34.         public string SecondName { getset; }  
  35.   
  36.         [MaxLength(50)]  
  37.         public string Surname { getset; }  
  38.   
  39.         [Display(Name = "A.K.A")]  
  40.         [MaxLength(50)]  
  41.         public string AKA { getset; }  
  42.   
  43.         [Display(Name = "ID Number")]  
  44.         [MaxLength(50)]  
  45.         public string IdNumber { getset; }  
  46.   
  47.         [Display(Name = "Date of Birth")]  
  48.         //[DataType(DataType.Date)]  
  49.         [DisplayFormat(DataFormatString = "{dd/mm/yyyy}", ApplyFormatInEditMode = true)]  
  50.         public DateTime? DOB { getset; }  
  51.   
  52.         //Foreign Key from Gender Class  
  53.         [Display(Name = "Gender")]  
  54.         public int GenderId { getset; }  
  55.   
  56.         [ForeignKey("GenderId")]  
  57.         public virtual Gender Genders { getset; }  
  58.         //end  
  59.   
  60.         //Foreign Key from Group  
  61.         [Display(Name = "Group")]  
  62.         public int GroupId { getset; }  
  63.   
  64.         [ForeignKey("GroupId")]  
  65.         public virtual Group Groups { getset; }  
  66.         //end  
  67.   
  68.         //Foreign Key from Work Permit  
  69.         [Display(Name = "Work Permit")]  
  70.         public int WorkPermitId { getset; }  
  71.   
  72.         [ForeignKey("WorkPermitId")]  
  73.         public virtual WorkPermit WorkPermits { getset; }  
  74.         //end  
  75.   
  76.         [Display(Name = "Expiry Date")]  
  77.         public DateTime? WorkPermitExpireDate { getset; }  
  78.   
  79.         //Foreign Key from Passports  
  80.         [Display(Name = "Passport")]  
  81.         public int PassportId { getset; }  
  82.   
  83.         [ForeignKey("PassportId")]  
  84.         public virtual Passport Passports { getset; }  
  85.         //end  
  86.   
  87.         [Display(Name = "Expiry Date")]  
  88.         public DateTime? PassportExpiryDate { getset; }  
  89.   
  90.         //ForeignKey from Language  
  91.         [Display(Name = "Language")]  
  92.         public int LanguageId { getset; }  
  93.   
  94.         [ForeignKey("LanguageId")]  
  95.         public virtual Language Languages { getset; }  
  96.         //end  
  97.   
  98.         //ForeignKey from Marital Status  
  99.         [Display(Name = "Marital Status")]  
  100.         public int MaritalStatusId { getset; }  
  101.   
  102.         [ForeignKey("MaritalStatusId")]  
  103.         public virtual Marital Maritals { getset; }  
  104.         //end  
  105.   
  106.         [Display(Name = "Mobile")]  
  107.         public string EmployeeMobileNo { getset; }  
  108.   
  109.         [Display(Name = "Home")]  
  110.         public string EmployeeHomeNo { getset; }  
  111.   
  112.         [Display(Name = "Name & Surname")]  
  113.         public string EmergencyName { getset; }  
  114.   
  115.         [Display(Name = "Work")]  
  116.         public string EmergencyWorkNo { getset; }  
  117.   
  118.         [Display(Name = "Mobile")]  
  119.         public string EmergencyMobileNo { getset; }  
  120.   
  121.         [Display(Name = "Email")]  
  122.         [DataType(DataType.EmailAddress)]  
  123.         [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Email is not valid. Insert a correct email address.")]  
  124.         public string EmergencyEmail { getset; }  
  125.   
  126.         [Display(Name = "Postal Address")]  
  127.         public string PostalAddress { getset; }  
  128.   
  129.         [Display(Name = "City/Town")]  
  130.         public string PostalCityTown { getset; }  
  131.   
  132.         [Display(Name = "Postal Code")]  
  133.         public string PostalCode { getset; }  
  134.   
  135.         //ForeignKey from Province  
  136.         [Display(Name = "Province")]  
  137.         public int ProvinceId { getset; }  
  138.   
  139.         [ForeignKey("ProvinceId")]  
  140.         public virtual Province Provinces { getset; }  
  141.         //end  
  142.   
  143.         [Display(Name = "Physical Address")]  
  144.         public string PhysicalAddress { getset; }  
  145.   
  146.         [Display(Name = "City/Town")]  
  147.         public string PhysicalTownCity { getset; }  
  148.   
  149.         [Display(Name = "Postal Code")]  
  150.         public string PhysicalPostalCode { getset; }  
  151.   
  152.         //ForeignKey from Province  
  153.           [Display(Name = "Province")]  
  154.           public int ProvinceId { getset; }  
  155.   
  156.           [ForeignKey("ProvinceId")]  
  157.           public virtual Province Provinces { getset; }  
  158.         //end  
  159.     }  

 
My Province Class is;
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6.   
  7. namespace LMKSystem.Models  
  8. {  
  9.     public class Province  
  10.     {  
  11.         [Key]  
  12.         public int ProvinceId { getset; }  
  13.   
  14.         [Display(Name = "Province")]  
  15.         public string SAProvince { getset; }  
  16.     }  
  17. }  
My DbSet is as follows;
  1. public class ApplicationDbContext : IdentityDbContext<ApplicationUser>  
  2. {  
  3. public DbSet<Employee> Employees { getset; }  
  4. public DbSet<Title> Titles { getset; }  
  5. public DbSet<Gender> Genders { getset; }  
  6. public DbSet<Group> Groups { getset; }  
  7. public DbSet<WorkPermit> WorkPermits { getset; }  
  8. public DbSet<Passport> Passports { getset; }  
  9. public DbSet<Language> Languages { getset; }  
  10. public DbSet<Marital> Maritals { getset; }  
  11. public DbSet<Province> Provinces { getset; }  
  12. }  
How can I have one dropdown list for the postal address Province and one dropdown list for the physical address Province in the same class?
I am fairly new to dropdown lists.
Thanking you in advance.
Regards,
Kyle

Answers (1)