DropdownList with unique values MVC

4

My class Galeria has a foreign key with Album because there is a 1-n relation and each picture in the Galeria item has a Album . Given that, in the CMS that I created I give the possibility of the user to relate a photo with an Album at the time of upload. For this, my View looks like this:

<div class="editor-label">
   Cursos @Html.DropDownList("Album", ViewData["Cursos"] as SelectList)
</div>

And my Controller:

AlbumAplicacao bdAlbum;
   bdAlbum = AlbumAplicacaoConstrutor.AlbumAplicacaoEF();
   var list = new SelectList(bdAlbum.ListarTodos().OrderByDescending(x => x.Nome), "ID", "Nome");
   ViewData["Cursos"] = list;

My Domain:

public class CONGRESSO_Galeria
{
    public int ID { get; set; }

    public string Descricao { get; set; }

    public string Foto { get; set; }

    public virtual CONGRESSO_Album CONGRESSO_Album { get; set; }

}

My problem is that I'm getting several identical values in Dropdown. I tried using Trim() and filtering using Distinct() but did not succeed.

I noticed that the problem was generated because at the time of recording the item, I am recording an album again. I'm going to fix this, but I've already registered hundreds of items and thanks to that, I need to find a way to change the display.

    
asked by anonymous 04.08.2014 / 20:54

1 answer

2

Rafael.

Try this:

[Controller]
     var list = new SelectList(bdAlbum.ListarTodos().OrderByDescending(x => x.Nome).GroupBy(x => x.Nome.ToLower().Trim()).Select(y => y.First()) , "ID", "Nome");

This will bundle all equal values.

Correct is to prevent duplicate values from being inserted into your bank.

    
04.08.2014 / 21:10