title: How to pass data from radio buttons to a model that is made by another model ASP.NET Core 6 MVC
body: <p>I have a questionnaire with 4 radio buttons and I need to pass the answers to the model in order to insert the answers into the database. The problem is that the model that displays the questionnaire has another model inside. How can I pass the data into the model in the <code>[HttpPost]</code> in the controller?</p>
  <p>In a <code>Services</code> folder, I have these methods</p>
  <pre class="lang-cs prettyprint-override"><code>public async Task&lt;IEnumerable&lt;Examen&gt;&gt; ObtenerReactivosUnidad1()
  {
      using var connection = new SqlConnection(connectionString);
      var queryUnidad1 = &quot;SELECT * FROM Examen &quot; +
          &quot;               INNER JOIN Unidad1 ON Examen.PreguntaId = Unidad1.PreguntaId &quot; +
          &quot;               WHERE Examen.UnidadId = 'Unidad1' AND Examen.ExamenId = 'b005BeU2022'&quot;;
      return await connection.QueryAsync&lt;Examen&gt;(queryUnidad1);
  }
  
  public async Task&lt;IEnumerable&lt;Examen&gt;&gt; ObtenerReactivosUnidad2()
  {
      using var connection = new SqlConnection(connectionString);
      var queryUnidad2 = &quot;SELECT * FROM Examen &quot; +
          &quot;               INNER JOIN Unidad2 ON Examen.PreguntaId = Unidad2.PreguntaId &quot; +
          &quot;               WHERE Examen.UnidadId = 'Unidad2' AND Examen.ExamenId = 'b005BeU2022'&quot;;
      return await connection.QueryAsync&lt;Examen&gt;(queryUnidad2);
  } 
  </code></pre>
  <p>In the models, I have:</p>
  <pre class="lang-cs prettyprint-override"><code>public class Examen
  {
      public int Id { get; set; }
      public string PreguntaId { get; set; }
      public string Respuesta_Alumno { get; set; }
      public string Pregunta { get; set; }
      public string Respuesta1 { get; set; }
      public string Respuesta2 { get; set; }
      public string Respuesta3 { get; set; }
      public string Respuesta4 { get; set; }
  }
  
  public class ViewModel
  {
      public IEnumerable&lt;Examen&gt; ReactivosUnidad1 { get; set; }
      public IEnumerable&lt;Examen&gt; ReactivosUnidad2 { get; set; }
  }
  </code></pre>
  <p>And in the controller:</p>
  <pre class="lang-cs prettyprint-override"><code>public async Task&lt;IActionResult&gt; Examen()
  {            
      ViewModel mimodelo = new ViewModel();
      mimodelo.ReactivosUnidad1 = await repositorioExamen.ObtenerReactivosUnidad1();
      mimodelo.ReactivosUnidad2 = await repositorioExamen.ObtenerReactivosUnidad2();
      return View(mimodelo);
  }
  </code></pre>
  <p>This is the view:</p>
  <pre><code>@using ENCB_Placement_Test;
  @model ViewModel
  @{
      var contador = 1;
  }
   
  &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
  &lt;form asp-action=&quot;Examen&quot;&gt;
      @foreach (var reactivo in Model.ReactivosUnidad1)
      {
          &lt;div class=&quot;mb-3&quot;&gt;
              &lt;label class=&quot;form-label&quot;&gt;@contador . @reactivo.Pregunta&lt;/label&gt;
              &lt;div class=&quot;form-check&quot;&gt;
                  &lt;input class=&quot;form-check-input&quot; type=&quot;radio&quot; id=&quot;Respuesta_Alumno&quot; name=&quot;Respuesta_Alumno&quot; value=&quot;Respuesta1&quot; checked=&quot;checked&quot; /&gt;                
                  &lt;label class=&quot;form-check-label&quot;&gt;@reactivo.Respuesta1&lt;/label&gt;
              &lt;/div&gt;
              
              &lt;div class=&quot;form-check&quot;&gt;
                  &lt;input class=&quot;form-check-input&quot; type=&quot;radio&quot; id=&quot;Respuesta_Alumno&quot; name=&quot;Respuesta_Alumno&quot; value=&quot;Respuesta2&quot; /&gt;
                  &lt;label class=&quot;form-check-label&quot;&gt;@reactivo.Respuesta2&lt;/label&gt;
              &lt;/div&gt;
              
              &lt;div class=&quot;form-check&quot;&gt;
                  &lt;input class=&quot;form-check-input&quot; type=&quot;radio&quot; id=&quot;Respuesta_Alumno&quot; name=&quot;Respuesta_Alumno&quot; value=&quot;Respuesta3&quot; /&gt;               
                  &lt;label class=&quot;form-check-label&quot;&gt;@reactivo.Respuesta3&lt;/label&gt;
              &lt;/div&gt;
              
              &lt;div class=&quot;form-check&quot;&gt;
                  &lt;input class=&quot;form-check-input&quot; type=&quot;radio&quot; id=&quot;Respuesta_Alumno&quot; name=&quot;Respuesta_Alumno&quot; value=&quot;Respuesta4&quot; /&gt;
                  &lt;label class=&quot;form-check-label&quot;&gt;@reactivo.Respuesta4&lt;/label&gt;
              &lt;/div&gt;            
          &lt;/div&gt;
          contador++;
      }
      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Terminar&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>