我失去了对提交MVC 3我的数据 [英] I lost my data on submit MVC 3

查看:104
本文介绍了我失去了对提交MVC 3我的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体框架MVC 3应用程序。

I have a MVC 3 application with entity framework.

在我的网页我使用包含我使用的所有对象的自定义模型。该网页的完美呈现,但是当我preSS提交按钮,我的对象丢失数据。

In my page I use a custom Model that contains all objects I use. The page is rendered perfectly, but when I press the submit button my object loses the data.

这是我的自定义模式:

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 code:

this is my Razor Html code:

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

这是我的创造code,其中我把数据库中的数据。
在这一部分,我的目标controleacessomodel是空的。

This is my create code, where I put the data on database. Is in this part, that my object controleacessomodel is empty.

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

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

Why my object is empty after submit?

推荐答案

这是不可能使用 Foreach源循环结构的生成的ID将是不正确的,因此, MVC是不能够将这些值映射回模型。您需要使用代替循环:

It's not possible to use the Foreach loop construct as the generated ids will be not be correct, therefore MVC is not able to map the values back to the model. You need to use a for loop instead:

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

菲尔哈克写一个好的博客文章这件事。此外斯科特汉塞尔尔曼写了一个不错的<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow>帖子了。

Phil Haack write a good blog post about this. Also Scott Hanselman wrote a nice post too.

这篇关于我失去了对提交MVC 3我的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆