How to pass a Ienumerable Model to a Controller?

Asked

Viewed 672 times

1

I have a page that correctly lists my records. Plus I have a Html.BeginForm for each line,

@model IEnumerable<Generico.Dominio.TB_POSSIBILIDADE>

    @{
       ViewBag.Title = "";
    }


                @if (Model.Count() > 0)
                {

                    foreach (var item in Model)
                    {

                        using (Html.BeginForm("GravarRegistro", "Possibilidade", FormMethod.Post))
                        {

                            <div class="container">

                                <div class="col-md-3 form-group">
                                    @Html.DisplayFor(c => item.DESCRICAO01) | R$ @Html.DisplayFor(c => item.VALOR01)
                                    @Html.TextBoxFor(c => item.VALORAPOSTA1, new { placeholder = "valor ", @class = "form-control" })
                                </div>


                                <div class="col-md-3 form-group">
                                    @Html.DisplayFor(c => item.DESCRICAO02) | R$ @Html.DisplayFor(c => item.VALOR02)
                                    @Html.TextBoxFor(c => item.VALORAPOSTA2, new { placeholder = "valor ", @class = "form-control" })
                                </div>


                                <div class="col-md-3 form-group">
                                    @Html.DisplayFor(c => item.DESCRICAO03) | R$ @Html.DisplayFor(c => item.VALOR03)
                                    @Html.TextBoxFor(c => item.VALORAPOSTA3, new { placeholder = "valor ", @class = "form-control" })
                                </div>

                            </div>


                            <div class="modal-footer">
                                <div class="col-md-2 form-group">
                                    <input type="submit" name="btnEnviar" id="btnEnviar" value="Enviar para o caixa" class="btn btn-primary" />
                                </div>
                                <div class="col-md-1 form-group">
                                    <input type="reset" name="btnLimpar" id="btnLimpar" value="Limpar" class="btn btn-danger" />
                                </div>
                            </div>



                        }


                    }

                }

Problem: when I am filling the data and send the write I have in control:More table is coming empty:

  public ActionResult GravarRegistro(TB_POSSIBILIDADE tabela)
        {

            try
            {

                string idusuario = (string)Session["idusuario"];
                string titulo    = (string)Session["descricaotitulo"];


                var tTabela = new CaixaAplicacao();
                tTabela.Inseri(idusuario,titulo, tabela.DESCRICAO01,tabela.DESCRICAO02,tabela.DESCRICAO03,
                               tabela.VALOR01,tabela.VALOR02,tabela.VALOR03,tabela.VALORAPOSTA1,tabela.VALORAPOSTA2,
                               tabela.VALORAPOSTA3,tabela.VALORTOTAL1,tabela.VALORTOTALRETORNO );
            }
            catch (Exception)
            {
                TempData["Erro"] = "Não foi possivel gravar o registro.";
                return RedirectToAction("SelecionarPossibilidade", "Possibilidade", new { id = @Session["idpossibilidadde"] });

            }


            return RedirectToAction("SelecionarPossibilidade", "Possibilidade", new { id = @Session["idpossibilidadde"] });
        }

inserir a descrição da imagem aqui

html:generated;

         <div class="panel panel-default">
                <div class="panel-heading"><small>Paysandu PA x Juventude  </small> </div>
                <div class="panel-heading"><small>27/07/2016 19:30 (quarta-feira) </small> </div>
            </div>



            <div class="list-group">
                <a href="#" class="list-group-item active">
                    Vencedor do Encontro
                </a>
                <br />

<form action="/Possibilidade/GravarRegistro" method="post">                            <div class="container">

                                <div class="col-md-3 form-group">
                                    Casa | R$ 01,73
                                    <input class="form-control" data-val="true" data-val-number="The field VALORAPOSTA1 must be a number." data-val-required="O campo VALORAPOSTA1 é obrigatório." id="item_VALORAPOSTA1" name="item.VALORAPOSTA1" placeholder="valor " type="text" value="0" />
                                </div>


                                <div class="col-md-3 form-group">
                                    Empate | R$ 03,60
                                    <input class="form-control" data-val="true" data-val-number="The field VALORAPOSTA2 must be a number." data-val-required="O campo VALORAPOSTA2 é obrigatório." id="item_VALORAPOSTA2" name="item.VALORAPOSTA2" placeholder="valor " type="text" value="0" />
                                </div>


                                <div class="col-md-3 form-group">
                                    Fora | R$ 04,20
                                    <input class="form-control" data-val="true" data-val-number="The field VALORAPOSTA3 must be a number." data-val-required="O campo VALORAPOSTA3 é obrigatório." id="item_VALORAPOSTA3" name="item.VALORAPOSTA3" placeholder="valor " type="text" value="0" />
                                </div>

                            </div>
                            <div class="modal-footer">
                                <div class="col-md-2 form-group">
                                    <input type="submit" name="btnEnviar" id="btnEnviar" value="Enviar para o caixa" class="btn btn-primary" />
                                </div>
                                <div class="col-md-1 form-group">
                                    <input type="reset" name="btnLimpar" id="btnLimpar" value="Limpar" class="btn btn-danger" />
                                </div>
                            </div>
</form>
             </div>
  • Gypsy Morrison, could you help me with your great knowledge? I thank you

  • How was the HTML generated from these forms?

  • @Ciganomorrisonmendez, I added hmtl, more is generating right, see there, I am using Ienumerable because I am creating a form for each record line, because in some cases I will have more than 1

2 answers

1


The Binding is wrong. See the name of ids and of names.

id="item_VALORAPOSTA1" name="item.VALORAPOSTA1"
id="item_VALORAPOSTA2" name="item.VALORAPOSTA2"
id="item_VALORAPOSTA3" name="item.VALORAPOSTA3"

To function, the Binding it had to be like this:

id="VALORAPOSTA1" name="VALORAPOSTA1"
id="VALORAPOSTA2" name="VALORAPOSTA2"
id="VALORAPOSTA3" name="VALORAPOSTA3"

This is solved by placing your form in a Partial, for example:

_Formulario.cshtml

@model Generico.Dominio.TB_POSSIBILIDADE

@using (Html.BeginForm("GravarRegistro", "Possibilidade", FormMethod.Post))
{
    <div class="container">
        <div class="col-md-3 form-group">
            @Html.DisplayFor(c => Model.DESCRICAO01) | R$ @Html.DisplayFor(c => Model.VALOR01)
            @Html.TextBoxFor(c => Model.VALORAPOSTA1, new { placeholder = "valor ", @class = "form-control" })
        </div>

        <div class="col-md-3 form-group">
            @Html.DisplayFor(c => Model.DESCRICAO02) | R$ @Html.DisplayFor(c => Model.VALOR02)
            @Html.TextBoxFor(c => Model.VALORAPOSTA2, new { placeholder = "valor ", @class = "form-control" })
        </div>

        <div class="col-md-3 form-group">
            @Html.DisplayFor(c => Model.DESCRICAO03) | R$ @Html.DisplayFor(c => Model.VALOR03)
            @Html.TextBoxFor(c => Model.VALORAPOSTA3, new { placeholder = "valor ", @class = "form-control" })
        </div>

    </div>

    <div class="modal-footer">
        <div class="col-md-2 form-group">
            <input type="submit" name="btnEnviar" id="btnEnviar" value="Enviar para o caixa" class="btn btn-primary" />
        </div>
        <div class="col-md-1 form-group">
            <input type="reset" name="btnLimpar" id="btnLimpar" value="Limpar" class="btn btn-danger" />
        </div>
    </div>
}

And the foreach:

foreach (var item in Model)
{
    @Html.Partial("_Formulario", item)
}
  • Your answer was perfect, worked, just got a little doubt, the values that are filled in the screen as VALORAPOSTA1 VALORAPOSTA2 and VALORAPOSTA3, are sent to the control, Some more fields that are just description that already comes filled from the bank are passing empty, as example Model.DESCRICAO01 and Model.VALOR01, have to pick up these values also?

  • There is. You need to put the fields in the form as well, but I don’t see much use in saving descriptive. Still, if you really need to send these fields to the Controller, use @Html.TextBoxFor() or @Html.HiddenFor(), if you don’t want them to show up.

0

An option would be to change the way you are rendering the text box instead of:

@Html.TextBoxFor(c => item.VALORAPOSTA1, new { placeholder = "valor ", @class = "form-control" })

Try it like this:

@Html.TextBox("VALORAPOSTA1", item.VALORAPOSTA1, new { placeholder = "valor ", @class = "form-control" })

In doing so, the control name will match the property name of your class Model and by doing the POST you will receive the values in Controller.

  • i appreciate your reply, but I preferred to do using partial, more if you can help me in that: some fields that are just description that comes filled from the bank are passing empty, as example Model.DESCRICAO01 and Model.VALOR01, have to pick up these values too?

  • Once you get the fields in your Controller, you must fill in your Model class. Then in your View you enter the fields with the same name as the property of your Model, then when you do the Post it sends the fields. That is, you would do virtually the same way as you did with the VALORAPOSTA1 field. But, when you don’t want the field to be editable you wouldn’t use a Textbox, but another control just to show the text and a control that will store the value and will be useful to send it in Post,@Html.Hiddenfor() for example.

Browser other questions tagged

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