Filter search with Checkbox values

Asked

Viewed 146 times

1

For example, the User typed the title of a publication, and marked the generous Action and Adventure, or the user did not type any title, but wants to see all publications that fit these genres. How can I do this search query?

My tables:

Publicacao
Generos
PublicacaoGenero(IdPublicacao e IdGenero)

Action:

public ActionResult Publicacoes(PesquisaPublicacoesViewModel viewModel = null)
        {
            if (viewModel == null)
            {
                PesquisaPublicacoesViewModel p = new PesquisaPublicacoesViewModel();
                p.TodosOsGeneros = db.Generos.ToList();
                p.PublicacoesSelecionadas = db.Publicacao.Where(x => x.Titulo.StartsWith(p.Titulo) && x.Aprovado).ToList();
                return View(p);

            }

            viewModel.TodosOsGeneros = db.Generos.ToList();


            if (string.IsNullOrEmpty(viewModel.Titulo))
            {
                viewModel.PublicacoesSelecionadas = db.Publicacao.Where(p => p.Aprovado).ToList();

            }
            else
            {
                viewModel.PublicacoesSelecionadas = db.Publicacao.Where(p => p.Titulo.StartsWith(viewModel.Titulo) && p.Aprovado).ToList();

            }

            return View(viewModel);
        }

View:

@model AllFiction.ViewModels.PesquisaPublicacoesViewModel

@using(Html.BeginForm("Publicacoes","Publicacoes",FormMethod.Get))
{

@Html.TextBox("Titulo", null, new {id="txtTitulo" })


@Html.CheckBoxListFor(model => model.IdGenerosSelecionados,
                        model => model.TodosOsGeneros,
                        genero => genero.IdGenero,
                        genero => genero.Genero,
                        model => model.GenerosSelecionados)

}

@foreach (var p in Model.PublicacoesSelecionadas)
{
    @p.Usuario1.NomeCompleto
    @p.Titulo
}

Viewmodel

public class PesquisaPublicacoesViewModel
    {

        public string Titulo { get; set; }

        public IEnumerable<Publicacao> PublicacoesSelecionadas { get; set; }

        public string[] IdGenerosSelecionados { get; set; }
        public IList<Generos> TodosOsGeneros;
        public IList<Generos> GenerosSelecionados;

    }

1 answer

2


At the moment when the Viewmodel to the Controller, there are two ways to get the Ids of the selected genres:

  • Through the Press IdGenerosSelecionados;
  • Through the collection GenerosSelecionados.

To obtain the Ids of GenerosSelecionados, it is possible to use something like this:

var ids = viewModel.GenerosSelecionados.Select(g => g.GeneroId).ToList();

Using one or the other, the query would look like this:

viewModel.PublicacoesSelecionadas = db.Publicacao.Where(p => p.Aprovado && viewModel.IdGenerosSelecionados.Contains(p.PublicacaoGenero.GeneroId)).ToList();

Or else:

viewModel.PublicacoesSelecionadas = db.Publicacao.Where(p => p.Aprovado && ids.Contains(p.PublicacaoGenero.GeneroId)).ToList();

It’s up to you the way you prefer to use. Don’t forget to charge PublicacaoGenero using Include().

Ah, another thing: you can rewrite your Controller like this:

    public ActionResult Publicacoes(PesquisaPublicacoesViewModel viewModel = null)
    {
        if (viewModel == null)
        {
            PesquisaPublicacoesViewModel p = new PesquisaPublicacoesViewModel();
            p.TodosOsGeneros = db.Generos.ToList();
            p.PublicacoesSelecionadas = db.Publicacao.Where(x => x.Titulo.StartsWith(p.Titulo) && x.Aprovado).ToList();
            return View(p);
        }

        viewModel.TodosOsGeneros = db.Generos.ToList();
        viewModel.PublicacoesSelecionadas = db.Publicacao.Include(p => p.PublicacaoGenero).Where(p => p.Aprovado).ToList();

        if (viewModel.GenerosSelecionados != null && viewModel.GenerosSelecionados.Any()) {
            var ids = viewModel.GenerosSelecionados.Select(g => g.GeneroId).ToList();
            viewModel.PublicacoesSelecionadas = db.Publicacao.Where(p => viewModel.IdGenerosSelecionados.Contains(p.PublicacaoGenero.GeneroId)).ToList();
        }

        if (!string.IsNullOrEmpty(viewModel.Titulo))
        {
            viewModel.PublicacoesSelecionadas = viewModel.PublicacoesSelecionadas.Where(p => p.Titulo.StartsWith(viewModel.Titulo)).ToList();
        }

        return View(viewModel);
    }

PS: I have not tested this code. Some variation may occur.

  • The only problem is that I do not keep the Gender Id in the Post there table that part (p.Generoid) of the error. Do you think that the most correct, for this case, would be to directly associate the publication table with the Gender table? (eliminating the Publicacaogenero table)

  • I’ll change the answer.

  • If it is too much work, I can change the database without problem...

  • No need. I already updated it. Look how it turned out.

  • 1

    Now It Worked. Thanks for the help, and for the patience.

Browser other questions tagged

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