2013-04-15 45 views
0

我有一個帶有實體框架的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

是的,我有一個表格! –

回答

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

菲爾哈克寫出好blog post這一點。 Scott Hanselman也寫了一個不錯的post

+0

完美!我的問題解決了! 非常感謝penfold !!!非常非常感謝! –

相關問題