0

I have this line of code in my view but it doesn't work. It keeps throwing this error:

The ViewData item that has the key 'Material.Modelo.Categoria.Familia_Id' is of type 'System.Int32' but must be of type 'IEnumerable'

<%= Html.DropDownListFor(model => model.Material.Modelo.Categoria.Familia_Id, 
                                  Model.Familias, 
                                  " -- Seleccione -- ")%>

Model.Familias is actually a SelectList and model.Material.Modelo.Categoria.Familia_Id is an integer.

Any ideas?

Thnx

1 Answer 1

0

Html.DropDownListFor expects to be passed a collection (something that imlements IEnumberable) so that it can build a drop down of these items.

You're passing it a single integer.

Assuming you have a drop down with just one item in it then add that integer to a collection of some sort and pass the collection. A List, or any other collection implementing IEnumerable, will work fine.

Check out this post...

Populating ASP.NET MVC DropDownList

... for details on how to populate one of these.

Sign up to request clarification or add additional context in comments.

3 Comments

The first Param in DropDownListFor is the property in the model you want to send back to the controller that is selected, the SECOND one is the IEnumerable collection. It looks to me like she is doing it right.
Exactly... and actually I have this: <%= Html.DropDownListFor(model => model.Material.Modelo_Id, Model.Modelos," -- Seleccione -- ") %> and that works fine. So, what may be happening?
Notice the use of SelectList in the answer that I refer to. I think that's the difference.

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.