title: Using a checkbox list with Razor Pages for input to DB
body: <p>I want one of my inputs in a form to come from a list of checkboxes that the user selects. I've been going at this for several hours now, and I still don't understand what I need to do for this. Why is there so much help for MVC on this topic and hardly any for Razor?</p>
  <p><strong>.cshtml</strong></p>
  <pre><code>&lt;form method=&quot;post&quot;&gt;
      &lt;label asp-for=&quot;ServiceRqLd.JobType&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
      &lt;div class=&quot;form-check-inline&quot;&gt;
          @for (var i = 0; i &lt; Model.JobTypes.Count(); i++)
          {
              &lt;div class=&quot;form-check&quot;&gt;
                  &lt;input asp-for=&quot;@Model.JobTypes[i].Selected&quot; class=&quot;form-check-input&quot; /&gt;
                  &lt;label asp-for=&quot;@Model.JobTypes[i].Selected&quot;&gt;@Model.JobTypes[i].Text&lt;/label&gt;
                  &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.JobTypes[i].Value&quot; /&gt;
                  &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.JobTypes[i].Text&quot; /&gt;
              &lt;/div&gt;
          }
      &lt;/div&gt;
      &lt;span asp-validation-for=&quot;ServiceRqLd.JobType&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
  &lt;/form&gt;
  </code></pre>
  <p><strong>.cshtml.cs</strong></p>
  <pre><code>[BindProperty]
  public ServiceRqLd ServiceRqLd { get; set; }
  [BindProperty]
  public List&lt;string&gt; AreTypes { get; set; }
  
  public IActionResult OnGet()
  {
      JobTypes = new List&lt;SelectListItem&gt;()
      {
          new SelectListItem() { Text=&quot;Mechanical&quot;, Value=&quot;Mechanical&quot; },
          new SelectListItem() { Text=&quot;Electrical&quot;, Value=&quot;Electrical&quot; },
          new SelectListItem() { Text=&quot;Fluid Power&quot;, Value=&quot;Fluid Power&quot; },
          new SelectListItem() { Text=&quot;Programming&quot;, Value=&quot;Programming&quot; }
      };
  
      return Page();
  }
  
  public async Task&lt;IActionResult&gt; OnPostAsync()
  {
      _context.ServiceLeadInfo.Add(ServiceRqLd);
      ServiceRqLd.JobType = FilterSelected();
      await _context.SaveChangesAsync();
  }
  
  private string FilterSelected()
  {
      foreach (var c in JobTypes)
      {
          if (c.Selected)
          {
              SelectedTypes = c.Value + &quot;, &quot; + SelectedTypes;
          }
      }
  
      return SelectedTypes;
  }
  
  </code></pre>
  <p><strong>Model</strong></p>
  <pre><code>public class ServiceRqLd
  {
      public int ServiceRqLdID { get; set; }
      public string JobType { get; set; }
  }
  </code></pre>
  <p>This is all I have so far, but I have no idea what I'm doing with this. I neither understand how to properly display the list of checkboxes nor know how to set ServiceRqLd.JobType equal to the selected boxes (be it a concatenated string of the selected options or otherwise).</p>
  <p>I have received conflicting information on how to do this by setting the name of the checkbox the same or not and how the selected information is stored. Any bit of help would be greatly appreciated.</p>
  <p>EDIT: I have edited my code to what I have figured out thus far. The problem is that ServiceRqLd.JobType remains null after submitting the form. I don't believe I really understand what I have in the .cshtml file. Are all of these inputs necessary? Which one do I need to change so that the checked boxes are inputs to the JobType property of the model.</p>