Null Arriving Object List in Controller

Asked

Viewed 40 times

2

I have a question and hope that someone can help me. I am not being able to send objects from my View to the controller. Every time it comes, it’s empty. I’ve tried a lot of things and I can’t seem to.

This is the Controller

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

Here is the View that is filled by the user, note that only the Field Valor needs to be filled in, the others already coming filled in.

@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 occur of arriving more than one, for I had the idea of putting as parameter in the controller a List of that 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.

1 answer

2


Your controller expects a list of objectives. The way you are assembling input names is only sending a single object. Simply modify the names of the inputs by grouping them. Ex: [0]. Mes

@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>
 } 
  • It worked perfectly, thank you. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.