How do I give a sort in my dropdownlist

5

How do I sort in my dropdown? This is my dropdown. How do I sort it out?

Calling DropDown

<select class="form-control col-sm-6" name="cbxCodTipo" id="cbxCodTipo"></select>

Of course I'll upload the values, but I just want to know how to order via Asp.Net only. I put down my complete combo. Sort by status (Description).

Riding DropDown

<div class="col-md-6">
            <select class="form-control col-sm-6" name="cbxCodTipo" id="cbxCodTipo" >
                <option value="AC">ACRE</option>
                <option value="AL">ALAGOAS</option>
                <option value="AM">AMAZONAS</option>
                <option value="AP">AMAPÁ</option>
                <option value="BA">BAHIA</option>
                <option value="CE">CEARÁ</option>
                <option value="DF">DISTRITO FEDERAL</option>
                <option value="ES">ESPÍRITO SANTO</option>
                <option value="GO">GOIÁS</option>
                <option value="MA">MARANHÃO</option>
                <option value="MG">MINAS GERAIS</option>
                <option value="MS">MATO GROSSSO DO SUL</option>
                <option value="MT">MATO GROSSO</option>
                <option value="PA">PARÁ</option>
                <option value="PB">PARAÍBA</option>
                <option value="PE">PERNMBUCO</option>
                <option value="PI">PIAUÍ</option>
                <option value="PR">PARANÁ</option>
                <option value="RJ">RIO DE JANEIRO</option>
                <option value="RN">RIO GRANDE DO NORTE</option>
                <option value="RO">RONDÔNIA</option>
                <option value="RR">RORAIMA</option>
                <option value="RS">RIO GRANDE DO SUL</option>
                <option value="SC">SANTA CATARINA</option>
                <option value="SE">SERGIPE</option>
                <option value="SP">SÃO PAULO</option>
                <option value="TO">TOCANTINS</option>
            </select>
        </div>
    
asked by anonymous 18.08.2014 / 21:44

2 answers

5

Try the following:

@{
    var estados = new List<ListItem>
    {
        new ListItem { Value = "AC", Text="ACRE"},
        new ListItem { Value = "AL", Text="ALAGOAS"},
        new ListItem { Value = "AM", Text="AMAZONAS"},
        new ListItem { Value = "AP", Text="AMAPÁ"},
        new ListItem { Value = "BA", Text="BAHIA"},
        new ListItem { Value = "CE", Text="CEARÁ"},
        new ListItem { Value = "DF", Text="DISTRITO FEDERAL"},
        new ListItem { Value = "ES", Text="ESPÍRITO SANTO"},
        new ListItem { Value = "GO", Text="GOIÁS"},
        new ListItem { Value = "MA", Text="MARANHÃO"},
        new ListItem { Value = "MG", Text="MINAS GERAIS"},
        new ListItem { Value = "MS", Text="MATO GROSSSO DO SUL"},
        new ListItem { Value = "MT", Text="MATO GROSSO"},
        new ListItem { Value = "PA", Text="PARÁ"},
        new ListItem { Value = "PB", Text="PARAÍBA"},
        new ListItem { Value = "PE", Text="PERNMBUCO"},
        new ListItem { Value = "PI", Text="PIAUÍ"},
        new ListItem { Value = "PR", Text="PARANÁ"},
        new ListItem { Value = "RJ", Text="RIO DE JANEIRO"},
        new ListItem { Value = "RN", Text="RIO GRANDE DO NORTE"},
        new ListItem { Value = "RO", Text="RONDÔNIA"},
        new ListItem { Value = "RR", Text="RORAIMA"},
        new ListItem { Value = "RS", Text="RIO GRANDE DO SUL"},
        new ListItem { Value = "SC", Text="SANTA CATARINA"},
        new ListItem { Value = "SE", Text="SERGIPE"},
        new ListItem { Value = "SP", Text="SÃO PAULO"},
        new ListItem { Value = "TO", Text="TOCANTINS"}
    };
    estados = estados.OrderBy(x=>x.Text).ToList();
}


@Html.DropDownList("cbxCodTipo", new SelectList(estados, "Value", "Text"), new { @class = "form-control col-sm-6" })
    
18.08.2014 / 22:01
2

Extending the @DiegoZanardo response using HtmlHelper of Razor :

@Html.DropDownListFor(model => model.CodTipo, new List<SelectListItem>
{
    new SelectListItem { Value = "AC", Text="ACRE"},
    new SelectListItem { Value = "AL", Text="ALAGOAS"},
    new SelectListItem { Value = "AM", Text="AMAZONAS"},
    new SelectListItem { Value = "AP", Text="AMAPÁ"},
    new SelectListItem { Value = "BA", Text="BAHIA"},
    new SelectListItem { Value = "CE", Text="CEARÁ"},
    new SelectListItem { Value = "DF", Text="DISTRITO FEDERAL"},
    new SelectListItem { Value = "ES", Text="ESPÍRITO SANTO"},
    new SelectListItem { Value = "GO", Text="GOIÁS"},
    new SelectListItem { Value = "MA", Text="MARANHÃO"},
    new SelectListItem { Value = "MG", Text="MINAS GERAIS"},
    new SelectListItem { Value = "MS", Text="MATO GROSSSO DO SUL"},
    new SelectListItem { Value = "MT", Text="MATO GROSSO"},
    new SelectListItem { Value = "PA", Text="PARÁ"},
    new SelectListItem { Value = "PB", Text="PARAÍBA"},
    new SelectListItem { Value = "PE", Text="PERNMBUCO"},
    new SelectListItem { Value = "PI", Text="PIAUÍ"},
    new SelectListItem { Value = "PR", Text="PARANÁ"},
    new SelectListItem { Value = "RJ", Text="RIO DE JANEIRO"},
    new SelectListItem { Value = "RN", Text="RIO GRANDE DO NORTE"},
    new SelectListItem { Value = "RO", Text="RONDÔNIA"},
    new SelectListItem { Value = "RR", Text="RORAIMA"},
    new SelectListItem { Value = "RS", Text="RIO GRANDE DO SUL"},
    new SelectListItem { Value = "SC", Text="SANTA CATARINA"},
    new SelectListItem { Value = "SE", Text="SERGIPE"},
    new SelectListItem { Value = "SP", Text="SÃO PAULO"},
    new SelectListItem { Value = "TO", Text="TOCANTINS"}
}.OrderBy(x => x.Text).ToList(), "Escolha", new { @class = "form-control" })
    
18.08.2014 / 22:54