title: How to save values when using checkbox and countrylist in ASP.NET MVC?
body: <p>I can't seem to save my values (checkbox and country list) to the table. I need help to improve my logic below, meaning they appear as NULL from the table on my database? What needs to change based on the given logic? I want if I select e.g Halaal and Vegetarian both should show from the table within the database the same applies with countryList(Afhaganistan/Ghana).</p>
  <pre><code>    // Controller for CountryList
       private List&lt;RegistrationTrainingForm&gt; LoadData()
              {
                  List&lt;RegistrationTrainingForm&gt; lst = new List&lt;RegistrationTrainingForm&gt;();
      
                  try
                  {
                      string line = string.Empty;
                      string srcFilePath = &quot;Content/files/country_list.txt&quot;;
                      var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                      var fullPath = Path.Combine(rootPath, srcFilePath);
                      string filePath = new Uri(fullPath).LocalPath;
                      StreamReader src = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));
      
                      // while to read the file
                      while ((line = src.ReadLine()) != null)
                      {
                          RegistrationTrainingForm infoLst = new RegistrationTrainingForm();
                          string[] info = line.Split(',');
      
                          //Setting
                          infoLst.Country_Id = Convert.ToInt32(info[0].ToString());
                          infoLst.Country_Name = info[1].ToString();
      
                          lst.Add(infoLst);
                      }
                      src.Dispose();
                      src.Close();
                  }
                  catch (Exception ex)
                  {
                      Console.Write(ex);
                  }
                  return lst;
              }
      
      
              // List for countries.
              private IEnumerable&lt;SelectListItem&gt; GetCountryList()
              {
                  SelectList listcn = null;
                  try
                  {
                      var list = this.LoadData().Select(p =&gt; new SelectListItem
                      {
                          Value = p.Country_Id.ToString(),
                          Text = p.Country_Name
                      });
                      listcn = new SelectList(list, &quot;Value&quot;, &quot;Text&quot;);
      
                  }
                  catch (Exception ex)
                  {
                      throw ex;
                  }
                  return listcn;
              }
  
  
  // Controller to SaveChanges to DB.
   //GET:TrainingRegForm/Create/WebRequest.
          [HttpPost]
         
          public ActionResult SubmitRegDetails(RegViewAndRoleViewModel eNtsaRegistration)
          {
             
  
              if(ModelState.IsValid)
              {
                 
                  db.TrainingRegs.Add(eNtsaRegistration.RegForm);
                  db.SaveChanges();
                  return RedirectToAction(&quot;SaveRegForm&quot;);
              }
  
              // Validates when empty.
              if(ModelState.IsValid)
              {
                  return RedirectToAction(&quot;SaveRegForm&quot;);
              }
              return View(eNtsaRegistration);
          }
  
      
      // Model for both Checkbox and CountryList
      public class DietViewModel
          {
      
              [Display(Name = &quot;None&quot;)]
      
              [Range(typeof(bool), &quot;true&quot;, &quot;true&quot;)]
              public bool None { get; set; }
      
              [Display(Name = &quot;Vegetarian&quot;)]
      
              [Range(typeof(bool), &quot;true&quot;, &quot;true&quot;)]
      
              public bool Vegetarian { get; set; }
      
              [Display(Name = &quot;Vegan&quot;)]
      
              [Range(typeof(bool), &quot;true&quot;, &quot;true&quot;)]
              public bool Vegan { get; set; }
      
              [Display(Name = &quot;Halaal&quot;)]
      
              [Range(typeof(bool), &quot;true&quot;, &quot;true&quot;)]
              public bool Halaal { get; set; }
      
              [Display(Name = &quot;Other&quot;)]
      
              [Range(typeof(bool), &quot;true&quot;, &quot;true&quot;)]
              public bool Other { get; set; }
      
      
          }
       public class DropDownViewModel
          {
              public string Title { get; set; }
              public string FirstName { get; set; }
      
              public string LastName { get; set; }
      
              public string Position { get; set; }
      
              public string Company { get; set; }
      
              public string StreetAddress { get; set; }
      
              public string StreetAddressLine { get; set; }
      
              public string City { get; set; }
      
              public string State { get; set; }
      
              public string Country { get; set; }
      
              public int ZipCode { get; set; }
      
              public string Email { get; set; }
      
              public int CellNumber { get; set; }
      
              public string DietaryRequirements { get; set; }
      
              public int Country_Id { get; set; }
      
              public string Country_Name { get; set; }
          }
      
      // View for both CountryList and Checkbox
      &lt;div class=&quot;form-group row&quot;&gt;
                                  &lt;label class=&quot;col-sm-2 col-form-label&quot;&gt;Dietary requirements:&lt;/label&gt;
                                  &lt;div class=&quot;col-sm-3&quot;&gt;
                                      &lt;div class=&quot;form-check&quot;&gt;
                                          @Html.CheckBoxFor(model =&gt; model.DietMain.None, new { @class = &quot;form-check-input&quot; })
                                          &lt;label class=&quot;form-check-label&quot; for=&quot;@Html.IdFor(m =&gt; m.DietMain.None)&quot;&gt;None&lt;/label&gt;
                                      &lt;/div&gt;
                                      &lt;div class=&quot;form-check&quot;&gt;
                                          @Html.CheckBoxFor(model =&gt; model.DietMain.Vegetarian, new { @class = &quot;form-check-input&quot; })
                                          &lt;label class=&quot;form-check-label&quot; for=&quot;@Html.IdFor(m =&gt; m.DietMain.Vegetarian)&quot;&gt;Vegetarian&lt;/label&gt;
                                      &lt;/div&gt;
  
                                      &lt;div class=&quot;form-check&quot;&gt;
                                          @Html.CheckBoxFor(model =&gt; model.DietMain.Vegan, new { @class = &quot;form-check-input&quot; })
                                          &lt;label class=&quot;form-check-label&quot; for=&quot;@Html.IdFor(m=&gt;m.DietMain.Vegan)&quot;&gt;Vegan&lt;/label&gt;
                                      &lt;/div&gt;
  
                                      &lt;div class=&quot;form-check&quot;&gt;
                                          @Html.CheckBoxFor(model =&gt; model.DietMain.Halaal, new { @class = &quot;form-check-input&quot; })
                                          &lt;label class=&quot;form-check-label&quot; for=&quot;@Html.IdFor(m=&gt;m.DietMain.Halaal)&quot;&gt;Halaal&lt;/label&gt;
                                      &lt;/div&gt;
  
                                      &lt;div class=&quot;form-check&quot;&gt;
                                          @Html.CheckBoxFor(model =&gt; model.DietMain.Other, new { @class = &quot;form-check-input&quot; })
                                          &lt;label class=&quot;form-check-label&quot; for=&quot;@Html.IdFor(m=&gt;m.DietMain.Other)&quot;&gt;Other&lt;/label&gt;
                                      &lt;/div&gt;
  
                                  &lt;/div&gt;
                              &lt;/div&gt;
      &lt;div class=&quot;col-sm-3&quot;&gt;
                                      @Html.DropDownListFor(m =&gt; m.ListCountries.Country, ViewBag.CountryList as SelectList)
                                  &lt;/div&gt;
  </code></pre>