How to bring in Razor a Dropdown with the element already selected from a database?

Asked

Viewed 67 times

-1

I created a register where an object has n elements and each of them is selected from a Dropdown, the registration works perfectly but when I edit the Dropdowns do not show the selected item according to what was saved in the Database:

Follow the view code:

                        for (var i = 0; i < Model.vehicletanks.Count; i++)
                        {
                            @Html.DropDownListFor(model => model.vehicletanks[i].fueltype, (IEnumerable<SelectListItem>)ViewBag.FuelTypeSelectList, Resources.Select, new { @class = "form-control" })

                        }

If I replace the "Dropdownlistfor" with an "Editorfor" it brings the value correctly, "fueltype" is an integer from 0 to 14.

1 answer

0

Yuri, try returning in your Viewbag a Selectlist. I don’t know if I understand this correctly, but you’re putting together a Dropdownlist sequence. I believe that the value you are bringing in Viewbag is not the corresponding.

View:

@Html.Dropdownlistfor(model => model.User, (Selectlist) Viewbag.Listausuario, "-- Please select", new { @class = "form-control" })

Controller:

Viewbag.Listausuario = new Selectlist(userRegra.Obtentodos(), "Id", "Login", Usuariopermissao.Usuarioid);

Whereas userRegra.Get all() returns Ienumerable

Browser other questions tagged

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