Reputation: 4410
I want to create dymanic controls in my ASP.NET MVC Project. For example
My Model contains an IList<Product> Products
. Every product in this list contains a new IList<ProductItem>
.
Product item has properties Text and Value.
Now i want to create one DropDownList for every Products and every dropdownlist
should contains items for ProductItem.
Is this possible with HtmlHelpers
?
Upvotes: 1
Views: 79
Reputation: 3641
If you don't want to use helpers, you can always do something like this:
<select>
@foreach (var x in Model)
{
<option value="@x.Value">@x.Text</option>
}
</select>
Upvotes: 0
Reputation: 2424
This is pretty straight forward. In your controller
public ActionResult Index()
{
List<Product> model = GetProductList();
View(model);
}
In your View:
@model IList<Products>
... and then later on ...
@Html.DropDownListFor(item => item.Name, new SelectList(Model, "Name", "Value"))
Upvotes: 2