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

后端 未结 9 1752
日久生厌
日久生厌 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<SelectListItem>)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<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> 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<SelectListItem>)ViewData["basetype"], "")%>
    
    0 讨论(0)
  • 2020-11-28 04:07

    For future readers, if you are using razor, try to change type of selectlist item from List to IEnumerable.

    From

    @Html.DropDownListFor(m => m.id, ViewBag.SomeList as List<SelectListItem>)
    

    To

    @Html.DropDownListFor(m => m.id, ViewBag.SomeList as IEnumerable<SelectListItem>)
    
    0 讨论(0)
  • 2020-11-28 04:08

    I had same problem, and finally I got the answer...

    The problem is that in the POST action, after submitting the form, the ModelState is not valid, or it's catching an error in try/catch, so the View is returned. But this time the View has not the ViewData["basetype"] correctly set.

    You need to populate it again, probably with the same code used before, so repeat this:

    var db = new DB();
    IEnumerable<SelectListItem> basetypes = db.Basetypes.Select(
        b => new SelectListItem { Value = b.basetype, Text = b.basetype });
    ViewData["basetype"] = basetypes;
    

    before the return View(meal) in the [HttpPost] method.

    exactly this will solve your problem:

    [HttpPost]
    public ActionResult Create(Meal meal)
    {
        if (ModelState.IsValid)
        {
            try
            {
                // TODO: Add insert logic here
                var db = new DB();
                db.Meals.InsertOnSubmit(meal);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                var db = new DB();
                IEnumerable<SelectListItem> basetypes = db.Basetypes.Select(
                   b => new SelectListItem { Value = b.basetype, Text = b.basetype });
                ViewData["basetype"] = basetypes;
                return View(meal);
            }
        }
        else
        {
            var db = new DB();
            IEnumerable<SelectListItem> basetypes = db.Basetypes.Select(
                b => new SelectListItem { Value = b.basetype, Text = b.basetype });
            ViewData["basetype"] = basetypes;
            return View(meal);
        }
    }
    

    I know this question is very old, but I came here today with the same problem, so other could come here later...

    0 讨论(0)
提交回复
热议问题