How I give a Sort on my dropdownlist

Asked

Viewed 124 times

5

How do I give a Sort to my dropdown? This is my dropdown. How do I give a Sort to it?

Calling to the Dropdown

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

Of course I will load the values, but I just want to know how to sort via Asp.Net only. I put down my complete combo. Sort by state(Description).

Riding the 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>
  • Pnet, your question has been answered?

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" })

2

Extending the @Diegozanardo response using the 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" })

Browser other questions tagged

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