1
I am developing a project in ASP.net MVC. In my registration form, I have a checkbox where I select the items I would like to add in the BD. My edit form, I have to get these checked items and show in the view: all the items that are on the grid and the fields that were selected that are saved in the BD. I would like to return to the View all checked items that are saved in the BD.
The code is like this:
@using Forte.Rastreador.ViewModels
@using GridMvc.Html
@model SuperModulosPerfilUsuarioViewModel
<fieldset>
    @Html.Label("Nome do Perfil: ")
    @Html.TextBoxFor(u => u.Descricao)
    <br /><br />
</fieldset>
<fieldset> //minha checkBOX
    <legend>Modulos do Sistema</legend>
    @Html.Grid(Model.ModulosSistemas).Columns(columns =>
    {
        columns.Add()
            .Encoded(false)
            .Sanitized(false)
            .SetWidth(30)
            .RenderValueAs(o => Html.CheckBox("Checked", @Model.Check, new { value = o.CodModulo }));
        columns.Add(u => u.DesModulo)
          .Titled("Modulos Perfil")
          .Encoded(false);
    })
</fieldset>
<br /><br />
Controller:
//Action metodo get Editar, onde retorna todo o conteudo de visualizacao para a view.
public ActionResult EditarPerfilUsuario(int CodPerfil)
{
        var perfilUsuario = PerfilUsuarioRepositorio.ObterPerfilUsuarioPorCodigo(CodPerfil);
        var perfilUsuarioVM = new SuperModulosPerfilUsuarioViewModel();
        perfilUsuarioVM.Descricao = perfilUsuario.Descricao;
        perfilUsuarioVM.ModulosSistemas = ModulosSistemaRepositorio.ListarModulosSistemas();
        perfilUsuarioVM.ModulosDoPerfil = ModulosPerfilRepositorio.ListarModulosDoPerfisPorCodPerfil(CodPerfil);
        foreach (var ms in perfilUsuarioVM.ModulosSistemas)
        {                
            foreach (var mp in perfilUsuarioVM.ModulosDoPerfil)
            {
                if (ms.CodModulo == mp.CodModulo)
                {
                    perfilUsuarioVM.Check = true;
                }
            }                
         }
        return View("EditarPerfilUsuario", perfilUsuarioVM);
    }
    public IEnumerable<ModulosSistema> ListarModulosSistemas()    //metodos listar que se encontram no meu repositorio
    {
        return this.Context.ModulosSistemas;
    }
    public IEnumerable<ModulosDoPerfil> ListarModulosDoPerfisPorCodPerfil(int CodPerfil)
    {
        return this.Context.ModulosDoPerfil.Where(c=>c.CodPerfil==CodPerfil);
    }
Is there any way to do this without using gridmvc?? in the foreach view???
– Hans Miller
Which version of MVC you are using?
– Randrade