I want to add Drop Down List html control to the web page fill it with products list. My Action controller looks like
public ActionResult Index()
{
return View(_repository.GetProducts(true));
}
Product model (Linq to SQL, below is partial class where SelectedId is for drop down selection id)
public partial class Product
{
public int SelectedId { get; set; }
}
The view is
@model IQueryable<Entity.Product>
@Html.DropDownListFor(.....)
What I do not understand how to fill up DropDownList with Products and if product selected bind Id to SelectedId property.
You can implement this like so
In your model you need something like this
In your Controller you need something like this
Then in your html (and this varies a bit depending on the view engine being used but lets assume razor) you need something like this
More information can be found on this site here