1

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?

2 Answers 2

2

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"))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! If i wrap it into an forech loop this will work i think.
Actually you don't have to wrap that in a for loop. In case you ARE in a for (or foreach) you can use just use @Html.DropDownList(...)
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.