1

I am having an issue on how to display a dropdown list of values in my database. My database table (Categories) contains 2 columns, which is the Id and Title of the category. The goal is to provide a dropdown list where a user can select a title that is stored in the table.

Model:

public partial class Category
{
  public int Id { get; set; }
  public string Title { get; set; }
}

Controller:

public ActionResult Index()
{
    private Context db = new Context();
    ViewBag.Category = new SelectList(db.Categories, "Id", "Title");
    return View();
}

View:

@model MyApp.Models.Category

<p>
     Categories: @Html.DropDownList("Id", "Select a Category")
</p>

There error I am getting is in the view that says:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Id'.
tereško
  • 58,060
  • 25
  • 98
  • 150
Shawn
  • 2,355
  • 14
  • 48
  • 98

3 Answers3

4

You are not passing the SelectList to the DropDownList() method but passing the attribute name that is id.

 @Html.DropDownList("Category", "Select a Category")

To explicitly pass the SelectList object using the overloaded DropDownList cast the ViewBag.Category to SelectList.

@Html.DropDownList("CategoryNameOfSelect", (SelectList)ViewBag.Category, "Select a Category")

public static MvcHtmlString DropDownList( this HtmlHelper htmlHelper, string name, IEnumerable selectList, string optionLabel )

htmlHelper Type: System.Web.Mvc.HtmlHelper The HTML helper instance that this method extends.

name Type: System.String The name of the form field to return.

selectList Type: System.Collections.Generic.IEnumerable A collection of SelectListItem objects that are used to populate the drop-down list.

optionLabel Type: System.String The text for a default empty item. This parameter can be null.

Adil
  • 146,340
  • 25
  • 209
  • 204
  • if we don't pass explicitly ViewBag.Category, it still works i guess – Ehsan Sajjad Apr 22 '14 at 04:46
  • 1
    I think we need to cast it as it gives error if we do not cast it, http://stackoverflow.com/questions/9642821/mvc3-dropdownlist-viewbag-issue – Adil Apr 22 '14 at 05:19
3

you are placing in viewbag's category key the dropdown items and in view you are passing "Id" that is the mistake you need to pass Category

Here you are saving it in Category key:

ViewBag.Category = new SelectList(db.Categories, "Id", "Title");

Try like this:

@Html.DropDownList("Category","Select a Category")
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Haha it worked. I don't really understand why it wanted Category passed in and not the Id. Will have to keep learning, but thank you! – Shawn Apr 22 '14 at 04:45
  • because you are setting ViewBag.Category in action so you need to pas the key name in DropDownList – Ehsan Sajjad Apr 22 '14 at 04:49
0

Try this way

@Html.DropDownList("Category", (SelectList)ViewBag.Category, "Select a Category")

OR

@Html.DropDownList("Category", ViewBag.Category as SelectList, "--Select a Category--")
Mohsin
  • 902
  • 3
  • 23
  • 49