0

What I want to do it's simple. I have a view that receives a model that contains 2 lists of 2 entities. I want to pass one of that lists to controller, when I call it. It sounds simple, and I already tried various solution proposed, but can't get this to work. Also I'm new to MVC.

The Model

public class SorteioEspecial
{
    RepositoryService service = new RepositoryService();

    public SorteioEspecial()
    {
        funcionario = new List<Funcionario>();
        ponderacaoFuncionario = new List<PonderacaoFuncionario>();
    }

    public IEnumerable<Funcionario> funcionario { get; set; }
    public IEnumerable<PonderacaoFuncionario> ponderacaoFuncionario { get; set; }

    public IEnumerable<Funcionario> GetFuncionarios()
    {
        funcionario = service.GetFuncionarios();
        return funcionario;
    }

    public IEnumerable<PonderacaoFuncionario> GetPonderacaoFuncionario()
    {
        ponderacaoFuncionario = service.GetPonderacaoFuncionario();
        return ponderacaoFuncionario;
    }


}

The View

    @model Alpd.Models.SorteioEspecial

@using (@Html.BeginForm("EditarPonderacoesEspecialSecond", "Sorteios",Model.ponderacaoFuncionario, FormMethod.Post))
{

  <div class="filterbox">
    <table class="table">
        <tr>
            <th>
                <b>ID</b>
            </th>
            <th>
                <b>ID Funcionário</b>
            </th>
            <th>
                <b>Nome Funcionário</b>
            </th>
            <th>
                <b>Ponderação</b>
            </th>
            <th>
                <b>Incluir Sorteio</b>
            </th>

            <th></th>
        </tr>

        @foreach (var item in Model.ponderacaoFuncionario)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.Id)</td>
                <td>@Html.DisplayFor(modelItem => item.Funcionario)</td>
                <td>@Html.DisplayFor(modelItem => item.Nome_Funcionario)</td>
                <td>@Html.DisplayFor(modelItem => item.Ponderacao)</td>
                <td>@Html.DisplayFor(modelItem => item.Incluir_sorteio)</td>
            </tr>
        }
    </table>

</div>
<!--
<div class="col-md-10">
    <a href='Url.Action("EditarPonderacoesEspecialSecond", "Sorteios", Model.ponderacaoFuncionario)'>
        <input type="submit" value="Editar Ponderações" class="btn-default" />
    </a>
</div>
-->


    @Html.HiddenFor(model => model.ponderacaoFuncionario)
    @Html.EditorFor(model => model.ponderacaoFuncionario)
    <input type="submit" value="Editar Ponderações" class="btn-default" />
}

The controller method

[HttpPost]
    public ActionResult EditarPonderacoesEspecialSecond (SorteioEspecial pf)
    {
        return View(pf);
    }

I just want to call the method, passing the list of entities ponderacaoFuncionario . Dont want to do anything on the view (just show the table). Also if I am not doing this by the proper way, can you give an advice of how to do it?

danielpm
  • 125
  • 5
  • 20
  • So you have model SorteioEspecial where do you initialize that class? And what is the exact issue? – Dnyanesh Feb 25 '15 at 17:31
  • Are you trying to make an action method in the controller? You are calling the method with a list of `SorteioEspecial` but the view only uses one of those as model, do you want to create a view for each, and what do you want to do with those in that case? – Guffa Feb 25 '15 at 17:36
  • @Guffa @ Dnyanesh This view is for show a table (to confirm a submission). On the view I will have a button to confirm, and with a click in this button I want to go to another method and pass it the entire ponderacaoFuncionario list (that will have item from the same entitie), to use in that method! – danielpm Feb 25 '15 at 17:42
  • @Guffa sorry, copied the wrong method. – danielpm Feb 25 '15 at 17:44

1 Answers1

0

As the view uses a SorteioEspecial you would create an instance of that class to put the list in and use as model:

[HttpPost]
public ActionResult EditarPonderacoesEspecialSecond (List<PonderacaoFuncionario> ponderacaoFuncionario)
{
    SorteioEspecial model = new SorteioEspecial();
    model.ponderacaoFuncionario = ponderacaoFuncionario;
    return View(model);
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Good point, forget that. But the problem is that pf that the method receive is null. It dont pass from the view to the controller! – danielpm Feb 25 '15 at 18:10
  • @danielpm: I'm not sure that you can recieve a list as model, you might need to wrap that in a class and use that in the form and as parameter. See: http://stackoverflow.com/questions/15375800/model-binding-to-a-list-mvc-4 – Guffa Feb 25 '15 at 18:29
  • Guffa, now its not null, but it is 0 (Count=0). If I put a break on foreach in my View, i can see that the list ponderacaoFuncionario is not null. Could be my form that isnt pass the list right? But i cant see why! – danielpm Feb 25 '15 at 18:35
  • @danielpm: You have the model both as route object in the `BeginForm` and as an `EditorFor`. You should check what each does in the generated page, so you can see which you can use. – Guffa Feb 25 '15 at 18:39
  • @Guffa: you can, as long as the HTML input names are in the form of `[N].Property`, where `N` is the index. – Chris Pratt Feb 25 '15 at 18:48
  • @Guffa don't matter wich I use, I get or null or Count = 0. At this point, I would be extremelly happy If I could pass the list of Model.ponderacaoFuncionarios.Id to the controller, but somehow I'm not achieving that also – danielpm Feb 26 '15 at 10:50
  • @ChrisPratt can you give me an example? – danielpm Feb 26 '15 at 10:51