Ricardo Dias
Ricardo Dias

Reputation: 1

HTML.DropDownList not showing the selected value

Please a really need some help. I getting crazy with this one.

I'm trying to build in a page 7 Tab's with 2 DropDown's in for selecting value. The list his always the same but in each tab i have a different selected value.

Everything goes well with the first one but i can't make the second one work.

Her is my Razor code:

 @Html.DropDownList("Q21_objectivos", new SelectList
    (Model.Q21_ObjectivosList, "Value", "Text", Model.Q21_Objs[i - 1]),
    "Seleccione um objectivo",
    new
    {
     id = "objectivo" + i.ToString(),
     @class = "form-control",
     @onchange = "detectarObjectivoDuplicado( " + i + ", $(this).val() );"
    })

This one is not working:

@Html.DropDownList("Q21_AvaliacaoLista",
new SelectList(Model.Q21_AvaliacaoLista, "Value", "Text",    Model.Q21_Pontuacao[i - 1]), 
new 
{
 id = "avalObjectivo" + i.ToString(),
 @class = "form-control",
 @onchange = "CalculaPontRes();"
})

Here is the resulting HTML:

<select class="form-control" id="objectivo1" name="Q21_objectivos" onchange="detectarObjectivoDuplicado( 1, $(this).val() );"><option value="">Seleccione um objectivo</option>
<option selected="selected" value="1">Preencher as tarefas em DotProject até ao dia 1 do mês seguinte</option>
<option value="2">Desenvolver aplicação SIADAP3</option>
<option value="3">Resolver 70% dos tickets atribuidos</option>
<option value="4">Desenvolver 2 aplicações locais em VB.net</option>
<option value="5">Desenvolver 2 aplicações locais em COBOL</option>
</select>

The resulting HTML of the not selecting dropdownlist:

<select class="form-control" id="avalObjectivo1" name="Q21_AvaliacaoLista" onchange="CalculaPontRes();"><option value="5">Superado (Pontuação 5)</option>
<option value="3">Atingido (Pontuação 3)</option>
<option value="1">Não Atingido (Pontuação 1)</option>
<option value="0">Avalie o Objectivo</option>
</select>

What can I say more... The model is loaded with the list as it's possible to see in the resulting HTML.

Upvotes: 0

Views: 778

Answers (2)

Hugo Agostinho
Hugo Agostinho

Reputation: 11

If you change the property name "Q21_AvaliacaoLista" to something else, the selectedvalue will work.

The property name (Q21_AvaliacaoLista) and the list of items (Model.Q21_AvaliacaoLista) cannot have the same name.

Maybe it is because of data biding of MVC...

Upvotes: 1

Ricardo Dias
Ricardo Dias

Reputation: 1

I just made it work by changing "Q21_AvaliacaoLista" to "Q21_Avaliacao" in the DropDownList that wasn't working.

Somehow the name was making probably a conflict. I don't understand why but this way it's working.

Upvotes: 0

Related Questions