【问题标题】:Pass list of Entities from View to controller将实体列表从视图传递到控制器
【发布时间】:2015-02-25 17:23:37
【问题描述】:

我想做的很简单。我有一个视图,它接收一个包含 2 个实体的 2 个列表的模型。当我调用它时,我想将其中一个列表传递给控制器​​。这听起来很简单,我已经尝试了各种提出的解决方案,但无法让它发挥作用。我也是 MVC 的新手。

模型

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;
    }


}

观点

    @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" />
}

控制器方法

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

我只想调用方法,传递实体列表 ponderacaoFuncionario 。不想在视图上做任何事情(只显示表格)。另外,如果我没有以正确的方式做到这一点,您能否就如何做到这一点提出建议?

【问题讨论】:

  • 所以你有模型 SorteioEspecial 你在哪里初始化那个类?确切的问题是什么?
  • 您是否尝试在控制器中创建操作方法?您正在使用 SorteioEspecial 列表调用该方法,但视图仅使用其中一个作为模型,您想为每个视图创建一个视图,在这种情况下您想对它们做什么?
  • @Guffa @Dnyanesh 此视图用于显示表格(确认提交)。在视图上,我将有一个按钮进行确认,单击此按钮我想转到另一个方法并将整个 ponderacaoFuncionario 列表(将具有来自同一实体的项目)传递给它,以在该方法中使用!
  • @Guffa 对不起,复制了错误的方法。

标签: c# asp.net-mvc model-view-controller


【解决方案1】:

由于视图使用SorteioEspecial,您将创建该类的实例以将列表放入并用作模型:

[HttpPost]
public ActionResult EditarPonderacoesEspecialSecond (List<PonderacaoFuncionario> ponderacaoFuncionario)
{
    SorteioEspecial model = new SorteioEspecial();
    model.ponderacaoFuncionario = ponderacaoFuncionario;
    return View(model);
}

【讨论】:

  • 好点,算了。但问题是方法接收的 pf 为空。它不会从视图传递到控制器!
  • @danielpm:我不确定您是否可以将列表作为模型接收,您可能需要将其包装在一个类中并在表单和参数中使用它。见:stackoverflow.com/questions/15375800/…
  • Guffa,现在它不是 null,而是 0(Count=0)。如果我在视图中中断 foreach,我可以看到 ponderacaoFuncionario 列表不为空。可能是我的表格没有通过列表吗?但我不明白为什么!
  • @danielpm:您将模型作为BeginForm 中的路由对象和EditorFor。您应该检查每个在生成的页面中的作用,以便查看可以使用哪些。
  • @Guffa:可以,只要HTML输入名称为[N].Property的形式,其中N是索引。
猜你喜欢
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
相关资源
最近更新 更多