How do you set the value and text values for a drop down list while at the same time binding it to a view model?
I have a list of quantities ranging from 1 thru 100. I want to pass this list of integers to my view and have them populated in a drop down list, and have the value and text values equal to the corresponding quantity. For example I am after something like this:
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
...
<option value="100">100</option>
</select>
I currently only get:
<select>
<option>1</option>
<option>2</option>
<option>3</option>
...
<option>100</option>
</select>
My view model:
public class ProductViewModel : ViewModelBase
{
public ProductViewModel()
{
Quantities = new List<int>();
for (int i = 1; i <= 100; ++i)
{
Quantities.Add(i);
}
}
public int Quantity { get; set; }
public List<int> Quantities { get; set; }
// Other view model properties left out
}
My controller:
public ActionResult Test()
{
ProductViewModel model = new ProductViewModel();
return View(model);
}
My view:
@model MyProject.Models.ProductViewModel
@Html.DropDownListFor(m => m.Quantity, new SelectList(Model.Quantities))