var brandsList = new List<SelectListItem>(){
new SelectListItem() { Text = "None", Value = "0" },
new SelectListItem() { Text = "Samsung", Value = "1" },
new SelectListItem() { Text = "Apple", Value = "2" },
new SelectListItem() { Text = "Microsoft", Value = "3" }
};
This is a phone class which i will be rendering a MVC razor view for.
public class Phone
{
public int Brand { get; set; }
public decimal Cost { get; set; }
public List<selectlistitem> BrandsList { get; set; }
}
Here, i am creating an "Create" action in my PhoneController.
In this i am assigning my brand list of items to the Phone models BrandList.
public ActionResult Create() {
var phone = new Phone();
var brandsList = new List<SelectListItem>(){
new SelectListItem() { Text = "None", Value = "0" },
new SelectListItem() { Text = "Samsung", Value = "1" },
new SelectListItem() { Text = "Apple", Value = "2" },
new SelectListItem() { Text = "Microsoft", Value = "3" }
};
phone.BrandsList = brandsList;
return View(phone);
}
Finally in my razor view i have a drowdownlistfor template with the first parameter as Brand property and the second parameter is the BrandList. @Html.DropDownListFor(model => model.Brand, Model.BrandsList) The view rendered shows this...
The dropdown options shown are...