Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 473.9k

Reference checkbox within db using asp.net mvc

Jul 20 2020 3:23 AM
HI Team
 
I need some help, i created a checkbox and countrylist, they both dont reference as data types objects on my table defition. When a person select e.g Countrylist(Ghana), checkbox(Halaal, Vegetarian). Who can show me how to achieve this? meaning when i select them from the form they dont get saved from the db. Both become NULL only instead of that value.
  1. // Controller  
  2. private List<RegistrationTrainingForm> LoadData()  
  3. {  
  4. List<RegistrationTrainingForm> lst = new List<RegistrationTrainingForm>();  
  5. try  
  6. {  
  7. string line = string.Empty;  
  8. string srcFilePath = "Content/files/country_list.txt";  
  9. var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);  
  10. var fullPath = Path.Combine(rootPath, srcFilePath);  
  11. string filePath = new Uri(fullPath).LocalPath;  
  12. StreamReader src = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));  
  13. // while to read the file  
  14. while ((line = src.ReadLine()) != null)  
  15. {  
  16. RegistrationTrainingForm infoLst = new RegistrationTrainingForm();  
  17. string[] info = line.Split(',');  
  18. //Setting  
  19. infoLst.Country_Id = Convert.ToInt32(info[0].ToString());  
  20. infoLst.Country_Name = info[1].ToString();  
  21. lst.Add(infoLst);  
  22. }  
  23. src.Dispose();  
  24. src.Close();  
  25. }  
  26. catch (Exception ex)  
  27. {  
  28. Console.Write(ex);  
  29. }  
  30. return lst;  
  31. }  
  32. // List for countries.  
  33. private IEnumerable<SelectListItem> GetCountryList()  
  34. {  
  35. SelectList listcn = null;  
  36. try  
  37. {  
  38. var list = this.LoadData().Select(p => new SelectListItem  
  39. {  
  40. Value = p.Country_Id.ToString(),  
  41. Text = p.Country_Name  
  42. });  
  43. listcn = new SelectList(list, "Value""Text");  
  44. }  
  45. catch (Exception ex)  
  46. {  
  47. throw ex;  
  48. }  
  49. return listcn;  
  50. }
  1. // Model    
  2. public class DropDownViewModel    
  3. {    
  4.         public string Title { getset; }    
  5.         public string FirstName { getset; }      
  6.         public string LastName { getset; }      
  7.         public string Position { getset; }      
  8.         public string Company { getset; }      
  9.         public string StreetAddress { getset; }      
  10.         public string StreetAddressLine { getset; }      
  11.         public string City { getset; }      
  12.         public string State { getset; }      
  13.         public string Country { getset; }      
  14.         public int ZipCode { getset; }      
  15.         public string Email { getset; }      
  16.         public int CellNumber { getset; }      
  17.         public string DietaryRequirements { getset; }      
  18.         public int Country_Id { getset; }      
  19.         public string Country_Name { getset; }    
  20. }    
  21.       
  22. public class DietViewModel    
  23. {      
  24.         [Display(Name = "None")]      
  25.         [Range(typeof(bool), "true""true")]    
  26.         public bool None { getset; }      
  27.         [Display(Name = "Vegetarian")]      
  28.         [Range(typeof(bool), "true""true")]      
  29.         public bool Vegetarian { getset; }      
  30.         [Display(Name = "Vegan")]      
  31.         [Range(typeof(bool), "true""true")]    
  32.         public bool Vegan { getset; }      
  33.         [Display(Name = "Halaal")]      
  34.         [Range(typeof(bool), "true""true")]    
  35.         public bool Halaal { getset; }      
  36.         [Display(Name = "Other")]      
  37.         [Range(typeof(bool), "true""true")]    
  38.         public bool Other { getset; }    
  39. }  
  1. // View  
  2. <div class="form-group row">  
  3. <label class="col-sm-2 col-form-label">Dietary requirements:</label>  
  4. <div class="col-sm-3">  
  5. <div class="form-check">  
  6. @Html.CheckBoxFor(m => m.DietMain.None, new { @class = "form-check-input" })  
  7. <label class="form-check-label" for="@Html.IdFor(m => m.DietMain.None)">None</label>  
  8. </div>  
  9. <div class="form-check">  
  10. @Html.CheckBoxFor(m => m.DietMain.Vegetarian, new { @class = "form-check-input" })  
  11. <label class="form-check-label" for="@Html.IdFor(m => m.DietMain.Vegetarian)">Vegetarian</label>  
  12. </div>  
  13. <div class="form-check">  
  14. @Html.CheckBoxFor(m => m.DietMain.Vegan, new { @class = "form-check-input" })  
  15. <label class="form-check-label" for="@Html.IdFor(m=>m.DietMain.Vegan)">Vegan</label>  
  16. </div>  
  17. <div class="form-check">  
  18. @Html.CheckBoxFor(m => m.DietMain.Halaal, new { @class = "form-check-input" })  
  19. <label class="form-check-label" for="@Html.IdFor(m=>m.DietMain.Halaal)">Halaal</label>  
  20. </div>  
  21. <div class="form-check">  
  22. @Html.CheckBoxFor(m => m.DietMain.Other, new { @class = "form-check-input" })  
  23. <label class="form-check-label" for="@Html.IdFor(m=>m.DietMain.Other)">Other</label>  
  24. </div>  
  25. </div>  
  26. </div>  

Answers (2)