I'm trying to create a DropDownList
on a razor view.
Would someone help me with this?
Normal HTML5 code:
<select id="dropdowntipo">
<option value="Exemplo1">Exemplo1</option>
<option value="Exemplo2">Exemplo2</option>
<option value="Exemplo3">Exemplo3</option>
</select>
I tried this:
@{
var listItems = new List<ListItem> {
new ListItem { Text = "Exemplo1", Value = "Exemplo1" },
new ListItem { Text = "Exemplo2", Value = "Exemplo2" },
new ListItem { Text = "Exemplo3", Value = "Exemplo3" }
};
}
@Html.DropDownListFor(model =>
model.tipo,
new SelectList(listItems),
"-- Select Status --"
)
//ViewModel
//Enum Class:
//Controller Action
//view Action
If you're using ASP.net 5 (MVC 6) or later then you can use the new Tag Helpers for a very nice syntax:
You can use this:
Using an array would be a little more efficient than creating a list.