0

我有一个带有实体框架的 MVC 3 应用程序。

在我的页面中,我使用了一个自定义模型,其中包含我使用的所有对象。页面完美呈现,但是当我按下提交按钮时,我的对象丢失了数据。

这是我的自定义模型:

public class ControleAcessoModel
{    
    private List<Controle> controles = new List<Controle>();

    public GRUPO_ACESSO_TB grupo_acesso_tb { get; set; }
    public List<Controle> Controles 
    {
        get 
        { 
            return controles; 
        } 
    }

    public void AddTela(byte id, string nome)
    {
        Controle ctrl = new Controle();
        ctrl.ID_TELA = id;
        ctrl.NM_TELA = nome;
        controles.Add(ctrl);
    }

    public class Controle
    {
       public bool Selecionado { get; set; }
       public byte ID_TELA { get; set; }
       public string NM_TELA { get; set; }
       public bool FL_SALVAR { get; set; }
       public bool FL_ALTERAR { get; set; }
       public bool FL_EXCLUIR { get; set; }
    }
}

这是我的 Razor Html 代码:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <table>
        <tr>
            <th>Salvar</th>
            <th>Editar</th>
            <th>Excluir</th>
            <th>Tela</th>
        </tr>
        @foreach (var item in Model.Controles)
        {
        <tr>
            <td style="text-align: center">
                @Html.EditorFor(modelItem => item.FL_SALVAR)
            </td>
            <td style="text-align: center">
                @Html.EditorFor(modelItem => item.FL_ALTERAR)
            </td>
            <td style="text-align: center">
                @Html.EditorFor(modelItem => item.FL_EXCLUIR)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NM_TELA)
            </td>
        </tr>
        }
        </table>        
        <p>
            <input type="submit" value="Salvar" />
        </p>
}

这是我的创建代码,我将数据放在数据库中。在这部分,我的对象 controleacessomodel 是空的。

[HttpPost]
public ActionResult Create(ControleAcessoModel controleacessomodel, byte id)
{
    if (ModelState.IsValid)
    {
        for (int i = 0; i < controleacessomodel.Controles.Count; i++)
        {
            if (ValidaSelecao(controleacessomodel.Controles[i]))
            {
                PERMISSAO_GRUPO_ACESSO_TELA_TB permissao = new PERMISSAO_GRUPO_ACESSO_TELA_TB();
                permissao.ID_GRUPO_ACESSO = controleacessomodel.grupo_acesso_tb.ID_GRUPO_ACESSO;
                permissao.ID_TELA = controleacessomodel.Controles[i].ID_TELA;
                permissao.FL_SALVAR = controleacessomodel.Controles[i].FL_SALVAR;
                permissao.FL_ALTERAR = controleacessomodel.Controles[i].FL_ALTERAR;
                permissao.FL_EXCLUIR = controleacessomodel.Controles[i].FL_EXCLUIR;
                db.PERMISSAO_GRUPO_ACESSO_TELA_TB.AddObject(permissao);
            }
        }                
        db.SaveChanges();
        return RedirectToAction("Edit", "GrupoAcesso", new { id = id });
    }

    return View(controleacessomodel);
}

为什么我的对象在提交后是空的?

4

1 回答 1

2

不可能使用Foreach循环构造,因为生成的 id 将不正确,因此 MVC 无法将值映射回模型。您需要改用for循环:

@for (int i = 0 ; i < Model.Controles.Count; i++)
{
<tr>
    <td style="text-align: center">
        @Html.EditorFor(m => m.Controles[i].FL_SALVAR)
    </td>
    <td style="text-align: center">
        @Html.EditorFor(m => m.Controles[i].FL_ALTERAR)
    </td>
    <td style="text-align: center">
        @Html.EditorFor(m => m.Controles[i].FL_EXCLUIR)
    </td>
    <td>
        @Html.DisplayFor(m => m.Controles[i].NM_TELA)
    </td>
</tr>
}

Phil Haack 写了一篇很好的博客文章。Scott Hanselman 也写了一篇不错的文章

于 2013-04-15T20:49:17.203 回答