6
I have a method to make changes in some data. Among these data, I have a dropdownlist which contains course periods. When I select to change, I need the dropdown comes with the option checked, as it is in the database.
I need him to come selected "Night" as it is in the bank:
Here is the select of dropdown. The @ViewBag.Periodos
I pass through the method Alterar
in the Controller:
<select name="selPeriodo" class="form-control">
@{
foreach (var item in @ViewBag.Periodos)
{
<option value="@item.Id">@item.Nome</option>
}
}
</select>
I believe you have no better answer
– Maicon Carraro
@Maiconcarraro I was going to comment on yours, but you excluded :(
– Leonel Sanches da Silva
Calm down, your answer is more complete so I have no reason to keep mine.
– Maicon Carraro
Manda a lot. It worked perfectly. Thanks @Ciganomorrisonmendez for the reply and the others too!
– Igor Macedo
Do I always have to go through using a Viewbag or is there some other way? I’m trying to implement something similar only using a Viewmodel, but I’m not getting it.
– Iago Frota
You can put the Dropdown values inside the Viewmodel, although this takes a little more work. Ask another question that I present to you the detailed approach.
– Leonel Sanches da Silva
@I asked a more detailed question about my problem. Follow the link http://answall.com/questions/184758/pega-um-item-selected-no-dropdownlistfor-usando-uma-viewmodel-asp-net-mvc
– Iago Frota