How to receive the various fields of a view in the controller

4

I need to create an Insert from multiple fields at the same time! My system has multiple table and relationships and I am having difficulties at that point. I have this registration screen: Withthiscode:

@using(Html.BeginForm()){<divstyle="width: 100%; float: left;">
        <table class="table table-bordered table-hover">
           <thead>
                <tr>
                    <th style="width:200px; text-align:center; vertical-align:middle;">Alunos</th>
                    <th style="width:150px; text-align:center; vertical-align:middle;">Atribuir Nota</th>
               </tr>
             </thead>
             <tbody>         
                    @{                   
                    for (int i = 0; i < Model.ListaMatriculas.Count(); i++)
                    {
                    <tr>
                    <td>@Model.ListaMatriculas[i].Aluno.Nome</td>

                        <td style="text-align:center;">
                                <input type="hidden" name="Matricula.IdMatricula" value="@Model.ListaMatriculas[i].IdMatricula" />
                                <input type="text" name="Nota" value="0" style="width: 30px;" /> 
                                 @Html.DropDownList("UnidadeCurricular.IdUnidadeCurricular", ViewBag.unidadecurricular_idunidadecurricular as SelectList, "")
                        </td>
                    </tr>
                    }
                    }

                </tbody>  
        </table>

        <div class="alert alert-success">
            <b>Obs: Notas devem possuir valores de 0 a 100.</b>        
        </div>
</div>     
<div style="width: 100%;">
    <button class="btn btn-sucsess " type="submit">Salvar</button>
</div>
}

However when I click on the send my controller does not receive the values of the fields, someone can give me a Help, how can I do something like this? An example link or something like this ...

    
asked by anonymous 15.10.2015 / 00:10

2 answers

2

If you want to keep the system with html pure or razor , modify the name attribute of your inputs and receive them as a list in your actions, as shown below.

Html

<tbody>         
   @{                   
   for (int i = 0; i < Model.ListaMatriculas.Count(); i++)
   {
     <tr>
        <td>@Model.ListaMatriculas[i].Aluno.Nome</td>    
        <td style="text-align:center;">
            <input type="hidden" name="Matriculas" value="@Model.ListaMatriculas[i].IdMatricula" />
            <input type="text" name="Notas" value="0" style="width: 30px;" /> 
            @Html.DropDownList("UnidadesCurriculares", ViewBag.unidadecurricular_idunidadecurricular as SelectList, "")
        </td>
     </tr>
  }
  }   
</tbody>

Razor

<tbody>         
   @{                   
   for (int i = 0; i < Model.ListaMatriculas.Count(); i++)
   {
     <tr>
        <td>@Model.ListaMatriculas[i].Aluno.Nome</td>    
        <td style="text-align:center;">
            @Html.Hidden("Matriculas",Model.ListaMatriculas[i].IdMatricula)
            @Html.TextBox("Notas")
            @Html.DropDownList("UnidadesCurriculares", ViewBag.unidadecurricular_idunidadecurricular as SelectList, "")
        </td>
     </tr>
  }
  }   
</tbody>

Controller

[HttpPost]
public ActionResult Foo(IList<int> Matriculas, IList<string> Notas, IList<int> UnidadesCurriculares)
{
     return View();
}

In this way, you will receive three parameter lists according to what is in your View. Note, however, that how the three lists are implemented are not interrelated. For this, it is necessary to use another approach.

    
15.10.2015 / 21:22
4

This is another classic case of using the BeginCollectionItem package

First, install the package via NuGet with the following command:

  

Install-Package BeginCollectionItem

After that, change your view to this:

@using (Html.BeginForm()){
<div style="width: 100%; float: left;">
        <table class="table table-bordered table-hover">
           <thead>
                <tr>
                    <th style="width:200px; text-align:center; vertical-align:middle;">Alunos</th>
                    <th style="width:150px; text-align:center; vertical-align:middle;">Atribuir Nota</th>
               </tr>
             </thead>
             <tbody>     
                  @foreach(var matricula in Model.ListaMatriculas)
                  {
                    Html.RenderPartial("_Matriculas",matricula);
                  }   
                </tbody>  
        </table>

        <div class="alert alert-success">
            <b>Obs: Notas devem possuir valores de 0 a 100.</b>        
        </div>
</div>     
<div style="width: 100%;">
    <button class="btn btn-sucsess " type="submit">Salvar</button>
</div>
}

Your view is now calling a partial that contains the data you need.

And your partialView _Matricula will look like this:

@model ModelMatriculaAqui

@using (Html.BeginCollectionItem("ListaMatriculas"))
{
  <tr>
  <td>@Model.ListaMatriculas[i].Aluno.Nome</td>
    <td style="text-align:center;">
             @Html.HiddenFor(model => model.IdMatricula)
              @Html.EditorFor(model => model.Nota)
             @Html.DropDownList("UnidadeCurricular.IdUnidadeCurricular", ViewBag.unidadecurricular_idunidadecurricular as SelectList, "")//Sugiro alterar para um dropDownListFor(), caso sua viewModel possua essa propriedade
    </td>
  </tr>
}

At rest, just continue to do the same.

  

Any questions, this problem already has various answers in this search.

    
15.10.2015 / 18:42