Pass List from View to a Controller

0

I have a view where data is displayed in which data can be entered (Notes)

@model IList<Proyecto01.Models.Nota>

@{
ViewBag.Title = "Notas";
}

<h2 class="text-center">    Notas</h2>
@using (Html.BeginForm("ActualizarNotas", "Nota", FormMethod.Post)) {

    

    <th>
        @Html.DisplayName("Nombres")
    </th>
    <th>
        @Html.DisplayName("Apellido Materno")
    </th>
    <th>
        @Html.DisplayName("Apellido Paterno")
    </th>

    <th>
        @Html.DisplayName("Nota 1")
    </th>
    <th>
        @Html.DisplayName("Nota 2")
    </th>
    <th>
        @Html.DisplayName("Nota 3")
    </th>
    <th>
        @Html.DisplayName("Nota 4")
    </th>
    <th>
        @Html.DisplayName("Nota 5")
    </th>

    <th>
        @Html.DisplayName("Promedio")
    </th>
    <th></th>
   </tr>

    @for (var i = 0; i < Model.Count; i++)
   {
    <tr>

        <td>
            @Html.DisplayFor(x=> x[i].nombres)
        </td>
        <td>
            @Html.DisplayFor(x=> x[i].apellidoMaterno)
        </td>
        <td>
            @Html.DisplayFor(x => x[i].apellidoPaterno)
        </td>
      @Html.HiddenFor(x=> x[i].rutAlumno)  
         @Html.HiddenFor(x=>x[i].idAsignatura)

        <td>
            @Html.TextBoxFor(x=>x[i].nota1, new { @class = "form-control ", 
          style = "width:50px;", maxlength = 20 })
        </td>
        <td>
            @Html.TextBoxFor(x => x[i].nota2, new { @class = "form-control 
       ", style = "width:50px;", maxlength = 20 })
        </td>
        <td>
            @Html.TextBoxFor(x => x[i].nota3, new { @class = "form-control 
      ", style = "width:50px;", maxlength = 20 })
        </td>
        <td>
            @Html.TextBoxFor(x => x[i].nota4, new { @class = "form-control 
      ", style = "width:50px;", maxlength = 20 })
        </td>
        <td>
            @Html.TextBoxFor(x => x[i].nota5, new { @class = "form-control 
       ", style = "width:50px;", maxlength = 20 })
        </td>
        <td>
            @Html.TextBoxFor(x => x[i].promedio, new { @class = "form-
       control ", @readonly = "readonly", style = "width:50px;", maxlength = 
      20 })
        </td>




    </tr>
 } 

 </table>
 }

  <td>
 <input type="submit" value="Guardar" class="btn btn-danger" /><br />
 </td>

How do I receive this data in the controller and be able to execute an Add method which stores this data in the database.

 [HttpPost]
    public ActionResult ActualizarNotas(ICollection<Nota> objetos)
    {


        Nota n = new Nota();



            n.actualizarNotas();



      return RedirectToAction("Notas","Nota");
  }
    
asked by Luis Ruiz Martínez 15.04.2017 в 08:16
source

1 answer

2

One way to do it would be to send it as an array:

 @for (var i = 0; i < Model.Count; i++)
   {
    <tr>

        <td>
            @Html.DisplayFor(x=> x[i].nombres, htmlAttributes: new{ name = "nombres[]" })
        </td>
       <td>
        @Html.DisplayFor(x=> x[i].apellidoMaterno, htmlAttributes: new{ name = "apellidoMaterno[]" } )
    </td>
 </tr>
}

And in the serious action:

  public ActionResult ActualizarNotas()
{
               string[] nombres = Request["nombres[]"].Split(',');
              string[] apellidos= Request["apellidoMaterno[]"].Split(',');

         List<Alumno> alumnos = new List<Alumno>();
         for(int i = 0; i < nombres.Length;i++)
         {
           Alumno a = new Alumnos();
           a.nombre = nombres[i];
           a.apellidoMaterno = apellidos[i];
           alumnos.Add(a);

        }

    }
    
answered by 25.04.2017 / 14:47
source