List of objects getting null in the Controller

I have a doubt and I hope someone can help me. It turns out that I am not being able to send objects from my peer View to the controller. Whenever it arrives, it is empty.. I've tried several things and I can't.

Here is Controller

public ActionResult PersistirValoresMetricas(List<ModeloAtualizacaoMetrica> modelo)
    {
        return View();
    }

Here is the View that is filled by the user, note that only the field value needs to be filled, the others already arriving filled.

@model IEnumerable<SigeApp.Models.ModeloAtualizacaoMetrica>


@using (Html.BeginForm("PersistirValoresMetricas", "Administracao", 
FormMethod.Post))
{ 
 <table class="table">

 <tr>
    <th> Nome </th>
    <th> Mes </th>
    <th> Ano </th>
    <th> Valor </th>
</tr>
@foreach (var item in Model)
{
    <tr>
        <td style="display:none"><input type="text" value="@item.Id_Metrica" name="Id_Metrica"></td>
        <td style="display:none"><input type="text" value="@item.Mes" name="Mes"    ></td>
        <td style="display:none"><input type="text" value="@item.Ano" name="Ano"></td>
        <td style="display:none"><input type="text" value="@item.Id_Planejamento" name="Id_Planejamento" ></td>

        <td>@Html.DisplayFor(x => item.TBMETRICA.Nome)</td>
        <td>@Html.DisplayFor(x => item.Mes)</td>
        <td>@Html.DisplayFor(x => item.Ano)</td>
        <td><input type="text" class="form-control" name="Valor" /></td>

    </tr>
}
<tr>
    <td>
        <input type="submit" value="Atualizar" class="btn btn-success" /> 
    </td>
</tr>
</table>
     }  

And this is the model that should arrive in the controller, it happens that it can happen to arrive more than one, so I had the idea to put as a parameter in the controller a list of this object

 public class ModeloAtualizacaoMetrica
 {

    public int Id_Metrica { get; set; }
    public int Mes { get; set; }
    public int Ano { get; set; }
    public decimal Valor { get; set; }
    public int Id_Planejamento { get; set; }


    public virtual TBMETRICA TBMETRICA { get; set; }
    public virtual TBPLANEJAMENTO TBPLANEJAMENTO { get; set; }


}

I appreciate the help.

Author: Jhony Adell, 2019-02-07

1 answers

Your controller expects a list of goals. The way you are assembling the input names is only being sent a single object. Simply modify the input names by grouping them. Ex: [0].Month

@model IEnumerable<SigeApp.Models.ModeloAtualizacaoMetrica>

@using (Html.BeginForm("PersistirValoresMetricas", "Administracao", FormMethod.Post))
{ 
    <table class="table">
        <tr>
            <th> Nome </th>
            <th> Mes </th>
            <th> Ano </th>
            <th> Valor </th>
        </tr>

        @{
            int i = 0;
            foreach (var item in Model)
            {
                <tr>
                    <td style="display:none"><input type="text" value="@item.Id_Metrica" name="[@i].Id_Metrica"></td>
                    <td style="display:none"><input type="text" value="@item.Mes" name="[@i].Mes"></td>
                    <td style="display:none"><input type="text" value="@item.Ano" name="[@i].Ano"></td>
                    <td style="display:none"><input type="text" value="@item.Id_Planejamento" name="[@i].Id_Planejamento" ></td>

                    <td>@Html.DisplayFor(x => item.TBMETRICA.Nome)</td>
                    <td>@Html.DisplayFor(x => item.Mes)</td>
                    <td>@Html.DisplayFor(x => item.Ano)</td>
                    <td><input type="text" class="form-control" name="[@i].Valor" /></td>

                </tr>

                i++;
            }
        }

        <tr>
            <td>
                <input type="submit" value="Atualizar" class="btn btn-success" /> 
            </td>
        </tr>
    </table>
 } 
 2
Author: Thiagosilr, 2019-02-07 16:16:19