Hi @Jon Jacobs ,
The format is:
@Help.DropDownFor(string, IEnumerable, string, object)
There are two ways:
First,if you have no classes,you could directly add to the view.Just like this:
@Html.DropDownListFor(model => model.Range,
new List<SelectListItem> {
new SelectListItem { Value = "0" , Text = "Range1" },
new SelectListItem { Value = "1" , Text = "Range2" },
new SelectListItem { Value = "2" , Text = "Range3" }
},
new { @class="myselect"})
Second,it fills from db context
In the controller:
MyViewModel model = new MyViewModel();
// Select the data to display (lots of ways to do this)
var states= from s in dbcontext.Range
where s.SomeProperty == someValue // if you want to filer the results
select s
// Assign select list (this sets the value to the ID property and the display text to the Name property)
model.StateList = new SelectList(states, "ID", "Name");
// and if you want to set a default
model.StateID = "NY";
return View(model);
In the View:
@Html.DropDownListFor(m => m.StateID, Model.StateList)
Best regards,
Yijing Sun
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.