The ViewData item that has the key 'MY KEY' is of type 'System.String' but must be of type 'IEnumerable'

后端 未结 9 1780
日久生厌
日久生厌 2020-11-28 03:02

I am trying to populate a dropdown list from a database mapped with Linq-2-SQL, using ASP.NET MVC 2, and keep getting this error.

I am so confused because I am decla

9条回答
  •  长情又很酷
    2020-11-28 04:05

    Try adding a string for the name of your dropdown list as the first parameter, and get the item out of your viewdata:

     <%= Html.DropDownList("SomeDropdownName", (IEnumerable)ViewData["basetype"]) %>
    

    Here is also an extension method you can use so the dropdown list is set up in a similar style to how you have done your other controls:

            public static string DropDownList(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string optionLabel)
            where TModel : class
        {
            string inputName = ExpressionHelper.GetInputName(expression);
            return htmlHelper.DropDownList(inputName, selectList, optionLabel);
        }
    

    For example

    <%= Html.DropDownList(x => x.BaseType, (IEnumerable)ViewData["basetype"], "")%>
    

提交回复
热议问题