无法将数据绑定到List

时间:2018-03-04 18:40:03

标签: asp.net-mvc list razor data-binding

我正在使用以下视图:

@model IEnumerable<Gestor.Models.VarTc>
@{
    ViewBag.Title = "Alterar TC";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Alterar TC</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

<table class="table table-hover">
<tr>
    <th>Item</th>
    <th>Descrição</th>
    <th>Critério</th>
    <th>Var TC1</th>
    <th>Var TC2</th>
    <th>Var TC3</th>
    <th>Var TC4</th>
</tr>

@foreach (var item in Model)
{
    @Html.HiddenFor(m => item.Id)

    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Apelido)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Descricao)
        </td>
        <td>
            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(model => item.Criterio, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => item.Criterio, "", new { @class = "text-danger" })
                </div>
            </div>
        </td>
        <td>
            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(model => item.VarTc1, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => item.VarTc1, "", new { @class = "text-danger" })
                </div>
            </div>
        </td>
        <td>
            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(model => item.VarTc2, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => item.VarTc2, "", new { @class = "text-danger" })
                </div>
            </div>
        </td>
        <td>
            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(model => item.VarTc3, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => item.VarTc3, "", new { @class = "text-danger" })
                </div>
            </div>
        </td>
        <td>
            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(model => item.VarTc4, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => item.VarTc4, "", new { @class = "text-danger" })
                </div>
            </div>
        </td>
    </tr>
}

</table>

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Gravar e Calcular" class="btn btn-default" />
    </div>
</div>
}

<div>
    @Html.ActionLink("Retornar a lista", "Index")
</div>

本动作称之为:

public ViewResult VarTc()
    {
        var tcs = new List<VarTc>();
        var lista = db.PlanejVendas.ToList();

        foreach (var item in lista)
        {
            var produto = db.Produtos.Single(p => p.Id == item.ProdutoId);
            var tc = new VarTc
            {
                Id = item.ProdutoId,
                Apelido = produto.Apelido,
                Descricao = produto.Descricao,
                Criterio = item.Criterio,
                VarTc1 = item.VartC1,
                VarTc2 = item.VarTc2,
                VarTc3 = item.VartC3,
                VarTc4 = item.VartC4
            };

            tcs.Add(tc);
        }

        return View(tcs);

邮政行动如下:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult VarTc(List<VarTc> varTc)
    {
        if (ModelState.IsValid)
        {
            foreach (var item in varTc)
            {
                var atual = db.PlanejVendas.Single(p => p.Id == item.Id);
                atual.Criterio = item.Criterio;
                atual.VartC1 = item.VarTc1;
                atual.VarTc2 = item.VarTc2;
                atual.VartC3 = item.VarTc3;
                atual.VartC4 = item.VarTc4;

                db.SaveChanges();
            }

            RedirectToAction("Index");
        }

        return RedirectToAction("VarTc");
    }

当执行中我填写表单并运行操作时,我验证没有List varTc。它实际上是空的。

因此,我至少相信列表中的值不会被视为返回。

我尝试了所有我能想到的东西,而且我无法获取数据。

如何处理数据?

是否与我尝试阅读列表或其他内容有关?

1 个答案:

答案 0 :(得分:1)

执行此操作时:

@foreach (var item in Model)
{
    @Html.HiddenFor(m => item.Id)
}

生成的所有html标签都具有相同的名称。在这种情况下,它们都将具有相同的name="Id", id="Id"。你不希望出现这种情况。这样做:

for (int i = 0; i < Model.Count; i++)
{
    @Html.HiddenFor(m => Model[i].Id)
}

您需要为所有字段执行此操作,以便每个字段都可以具有不同的name。然后,当您提交表单时,MVC活页夹会发现您有很多东西(VarTc),并且绑定将按预期工作。

如果您需要有关所遇问题的更多详细信息,请阅读this answer