How do I return to the View the items checked by the CheckBox?

1

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);
    }
    
asked by anonymous 13.02.2015 / 13:25

1 answer

0

Unfortunately I particularly find it bad to work with checkboxes and radiobuttons with MVC, but one of the ways I use it, I've set up this example to help you. Here is an example I made using the MVC helpers:

Control code:

     public class DisplayCheckBox
     {
        public int Valor { get; set; }
        public string Texto { get; set; }
        public bool Selecionado { get; set; }
     }

     public class FiltroController : Controller
     {
        public List<DisplayCheckBox> TodosChecks = new List<DisplayCheckBox>();

        public FiltroController()
        {
           for (int i = 0; i < 10; i++)
           {
              TodosChecks.Add(new DisplayCheckBox { Valor = i, Texto = "Nome " + i.ToString(), Selecionado = (i % 2 == 0) });
           }
        }

        //
        // GET: /Filtro/
        public ActionResult Index()
        {
           return View(new Formulario { Nome = "Teste", checados = TodosChecks });
        }

        [HttpPost]
        public ActionResult Index(Formulario modelo)
        {
           return View(modelo);
        }
     }

     public class Formulario
     {
        public string Nome { get; set; }
        public List<DisplayCheckBox> checados { get; set; }
     }

View code:

  @model MVCApp.Controllers.Formulario

  @using (Html.BeginForm())
  {
     <div class="form-horizontal">
        <div class="col-md-12">
           @Html.EditorFor(modelitem => modelitem.Nome)
        </div>
        <div class="col-md-12">
           <ul>
              @for (int i = 0; i < Model.checados.Count; i++)
              {
                 <li>
                    @Html.CheckBoxFor(modelitem => Model.checados[i].Selecionado)
                    @Model.checados[i].Texto

                    @Html.HiddenFor(modelitem => Model.checados[i].Valor)
                    @Html.HiddenFor(modelitem => Model.checados[i].Texto)
                 </li>
              }
           </ul>
        </div>

        <div class="col-md-12">
           <button class="btn" type="submit">Salvar</button>
        </div>
     </div>
  }

Note: In the example I only load the selected pairs (would be those of your database) and each post it sends the template with the selected property == true to those selected!

I hope I have helped !!

    
09.03.2015 / 21:22