我在提交MVC时丢失了我的数据3

问题描述:

我有一个带有实体框架的MVC 3应用程序。我在提交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; } 
    } 
} 

这是我的剃刀的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); 
} 

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

+1

你的视图中有“表格”吗? – pollirrata

+0

是的,我有一个表格! –

不可能使用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> 
} 

菲尔哈克写出好blog post这一点。 Scott Hanselman也写了一个不错的post

+0

完美!我的问题解决了! 非常感谢penfold !!!非常非常感谢! –