Domanda

@Html.DropDownListFor(model => model[i].InformationType, new List<SelectListItem>
{
   new SelectListItem{ Text = "Car", Value = "01" },
   new SelectListItem{ Text = "Home", Value = "02" },
   new SelectListItem{ Text = "Boat", Value = "03" }
}, "--none--")

The above creates a dropdown list in my view, but it does not open on the value being passed to the view. It always just defaults to --none--. Note that my model is an IList<>

Additionally, should I be coding these values in my model or database instead? If another value gets added, I wouldn't want to have to recompile my code, but I would rather the client not have to make a database call every time they want to open a dropdown.

È stato utile?

Soluzione

You need to set the selected item:

new SelectListItem{ Text = "Boat", Value = "03", Selected = @Model.SomeValue.Equals("03") }

It can be a good idea to create the List collection in the controller and set it as a property on the view model that gets passed to the view.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top