How to return to View items checked by Checkbox?

Asked

Viewed 1,642 times

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

  • Which version of MVC you are using?

1 answer

0

Unfortunately I especially find it bad to work with checkboxes and radiobuttons with MVC, but one of the ways I use this example to help you. Here’s an example I did 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 load only the selected pairs ( would be those of your database ) and each post it sends the template with the selected property == true for those selected!

I hope I’ve helped!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.