Nils Anders
Nils Anders

Reputation: 4410

Creating dynamic controls for project

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

Answers (2)

lopezbertoni
lopezbertoni

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

Mr. Young
Mr. Young

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

Related Questions