Model arriving empty in controller

Asked

Viewed 518 times

2

I basically have a Controller who sends a iList to the View, that one View could edit any record of that iList, but when the Controller receives it is 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 a @Html.Beginform inside for..

and then I did it on 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);
    }

Because the EP is coming empty? should I receive it as iList? but I just need a record..

1 answer

1


Your EP parameter is coming empty because your form in the View is not passing any value to your Action. Through the @Html.Displayfor() you are just displaying your values to the user, but you are not generating any kind 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 case of GET or by POST Data. When you put a proper type as parameter, it tries to find all parameters coming from HTTP with the same name as each property of its type to fill it.

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

If your goal when the user clicks on the "Record" button is to find out which of the items on your list received the action, then you need to pass an identifier from it 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 in 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
}
  • but this action would not conflict with what I already have, after all both receive an int Index(int variable)

  • and this Hiddenfor with (m => m) inside my for does not find nda...would be Model.Idestagioprocess ?

Browser other questions tagged

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