I'm developing an ASP.NET MVC project. In my registration form, I have a checkbox where I select the items that I would like to add in the DB. My edit form, I have to get these items checked and show in the view: all the items that are in the grid and the fields that were selected that are saved in the DB. I would like to return to View all checked items that are saved in DB.
The code looks 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);
}