Gcobani Mkontwana

Gcobani Mkontwana

  • 565
  • 1.9k
  • 408.2k

Dropdownlist is not working on my asp.net mvc?

Mar 17 2020 8:19 AM
Hi Team
 
I am trying to use this control on asp.net mvc, its throwing this error " System.InvalidOperationException: 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'SelectedCountryId'.'
 
  1. @Html.DropDownListFor(m=>m.SelectedCountryId, this.ViewBag.CountryList as SelectList, new {@class = "form-control"})  
  1. /** Controler**/     
  2.   
  3. // Selection for countries in the world.  
  4.   
  5.         public ActionResult DropDownSelect()  
  6.         {  
  7.             EditTrainingRegFormViewModel model = new EditTrainingRegFormViewModel();  
  8.   
  9.             model.SelectedCountryId = 0;  
  10.   
  11.             this.ViewBag.CountryList = this.GetCountryList();  
  12.             return this. View(model);  
  13.         }  
  14.   
  15.   
  16.      private List<EditTrainingRegFormViewModel> LoadData()  
  17.         {  
  18.             List<EditTrainingRegFormViewModel> lst = new List<EditTrainingRegFormViewModel>();  
  19.   
  20.             try  
  21.             {  
  22.                 string line = string.Empty;  
  23.                 string srcFilePath = "Content/files/country_list.txt";  
  24.                 var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);  
  25.                 var fullPath = Path.Combine(rootPath, srcFilePath);  
  26.                 string filePath =  new Uri(fullPath).LocalPath;  
  27.                 StreamReader src = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));  
  28.   
  29.                 // while to read the file  
  30.                 while((line = src.ReadLine()) !=null) {  
  31.                   EditTrainingRegFormViewModel infoLst = new EditTrainingRegFormViewModel();  
  32.                     string[] info = line.Split(',');  
  33.   
  34.                     //Setting  
  35.                     infoLst.Country_Id = Convert.ToInt32(info[0].ToString());  
  36.                     infoLst.Country_Name = info[1].ToString();  
  37.                       
  38.                     lst.Add(infoLst);  
  39.                 }  
  40.                 src.Dispose();  
  41.                 src.Close();  
  42.             }catch(Exception ex)  
  43.             {  
  44.                 Console.Write(ex);  
  45.             }  
  46.             return lst;  
  47.         }  
  48.   // List for countries.  
  49.         private IEnumerable<SelectListItem> GetCountryList()  
  50.         {  
  51.             SelectList listcn = null;  
  52.             try  
  53.             {  
  54.                 var list = this.LoadData().Select(p => new SelectListItem  
  55.                 {  
  56.                     Value = p.Country_Id.ToString(),  
  57.                     Text = p.Country_Name  
  58.                 });  
  59.                 listcn = new SelectList(list, "Value""Text");  
  60.   
  61.             }catch(Exception ex)  
  62.             {  
  63.                // throw ex;  
  64.             }  
  65.             return listcn;  
  66.         }  
  67.   
  68. // Model.cs  
  69.  public class EditTrainingRegFormViewModel  
  70.     {  
  71.         public string Title { getset; }  
  72.   
  73.         public string FirstName { getset; }  
  74.   
  75.         public string LastName { getset; }  
  76.   
  77.         public string Position { getset; }  
  78.   
  79.         public string Company { getset; }  
  80.   
  81.         public string Address { getset; }  
  82.   
  83.         [Display(Name = "Choose country")]  
  84.         public int ? SelectedCountryId { getset; }  
  85.   
  86.         public string Code { getset; }  
  87.   
  88.         public string City { getset; }  
  89.   
  90.         public string State { getset; }  
  91.   
  92.         public string Cell_Number { getset; }  
  93.   
  94.         public List<string> Dietary_requirement { getset; }  
  95.   
  96.         public string Email { getset; }  
  97.   
  98.         public int Country_Id { getset; }  
  99.   
  100.         public string Country_Name { getset; }  
  101.   
  102.   
  103.     }  
 

Answers (1)