Model arriving empty in the controller

2

I basically have a Controller that sends iList to View , this View could edit any record of this iList , however when Controller receives it already empty.

I did so:

Controller:

public ActionResult Index(int id)
{


    var epClientes = db.EstagioProcess
        .Include(etapa => etapa.Cliente)
        .GroupBy(etapa => etapa.ClienteId)
        .Where(grupo => grupo
            .OrderByDescending(etapa => etapa.EpId).Take(1)
            .Select(etapa => etapa.EP)
            .FirstOrDefault() == 2);

    var EP = new List<EstagioProcesso>();
    foreach (var epCliente in epClientes)
    {
        EP.AddRange(epCliente);
    }

    return View(EP);
}

View:

@model IList<WMB.MVC.Extranet.Models.EstagioProcesso>

<table class="table">
    <tr>
        <th>URL</th>
        <th>EP</th>
        <th>Data</th>
        <th>ID Funcionário</th>
        <th></th>
    </tr>
        @for (var i = 0; i < Model.Count; i++)
        {
            using (Html.BeginForm())
            {
                @Html.AntiForgeryToken()
        <tr>
            <td>
                @Html.DisplayFor(x => x[i].ClienteId)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].EP)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].Data)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].IdFuncionario)
            </td>
            <td>
                <input type="submit" value="Gravar" class="btn btn-default" />
            </td>
        </tr>
    }}
</table>

See that I put an @ Html.BeginForm inside the for ..

and then I made the controller to receive

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(EstagioProcesso EP)
    {
//EP Está vazio..não recebe, nda
        EP.EP = Convert.ToByte(Request.Form["Sel_EP"]);

        if (ModelState.IsValid)
        {
            db.EstagioProcess.Add(EP);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(EP);
    }

Why the EP is coming empty? Should I get it as an iList? but I just need a registry ..

    
asked by anonymous 05.02.2016 / 20:09

1 answer

1

Your EP parameter is arriving empty because your form in the View is not passing any value to your Action. Through @Html.DisplayFor () you are only displaying your values for the user, but you are not generating any type of HTML input field for your form to send something in the HTTP request.

The Actions parameters in MVC Controllers work only as facilitators to translate the parameters sent by the request, either by Query String in the case of GET or by POST Data. When you put your own type as a parameter, it tries to find all the parameters coming from HTTP with the same name of each property of its type to fill it.

This means that by the way you are filling your object in the first Action, it will probably not be healthy to try to pass it all through a request. Because of this, the most practical is that in the first Action you just get the values that need to be displayed and an identifier field so that you can retrieve any important information from it in the next Action.

If your goal when the user clicks the "Save" button is to find out which of the items on your list received the action, then you will need to pass an identifier through a Hidden input:

@model IList<WMB.MVC.Extranet.Models.EstagioProcesso>

<table class="table">
    <tr>
        <th>URL</th>
        <th>EP</th>
        <th>Data</th>
        <th>ID Funcionário</th>
        <th></th>
    </tr>
        @for (var i = 0; i < Model.Count; i++)
        {
            using (Html.BeginForm())
            {
                @Html.AntiForgeryToken()
        <tr>
            <td>
                @Html.DisplayFor(x => x[i].ClienteId)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].EP)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].Data)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].IdFuncionario)
            </td>
            <td>
                @Html.HiddenFor(m => m.IdEstagioProcesso) @* Aqui viria o campo com o valor que seria selecionado para identificar o seu objeto para consulta posterior *@
                <input type="submit" value="Gravar" class="btn btn-default" />
            </td>
        </tr>
    }}
</table>

And no Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(int idEstagioProcesso)
{
    // você receberá o valor submetido
    // como se trata de um identificador, você consegue recuperá-lo de alguma forma para usá-lo aqui
}
    
09.02.2016 / 01:53