tags:

views:

21

answers:

0

Hi there,

I am using MVC 2 and entity framework 4. On my create application page I have a drop down list which I populated with values from my AccountType enum. This is how I did it:

public ActionResult Create()
      {
         // Get the account types from the account types enum
         var accountTypes = from AccountType at
                            in Enum.GetValues(typeof(AccountType))
                            select new
                            {
                               AccountTypeID = (int)Enum.Parse(typeof(AccountType), at.ToString()),
                               AccountTypeName = GetEnumFriendlyName(at)
                            };
         ViewData["AccountTypes"] = new SelectList(accountTypes, "AccountTypeID", "AccountTypeName");

         return View();
      }

This is what my code looks like for this drop down list data:

<%= Html.DropDownList("AccountTypeID", (SelectList)ViewData["AccountTypes"], "-- Select --") %>

After the page loads I start to enter some values. I select a value from the drop down list. When all the required input is entered I click on submit. Below is just a piece of the code:

[HttpPost]
      public ActionResult Create(Application application)
      {
         if (ModelState.IsValid)
         {
            application.ApplicationState = (int)State.Applying;
         }

         return View();
      }

Then I get the following error, not sure what it means, but I did Google it, tried the samples, but I still get the message. Here is the error message:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'AccountTypeID'.

I even changed the drop down list in the view to:

<%= Html.DropDownList("AccountTypeID", (IEnumerable<SelectListItem>)ViewData["AccountTypes"], "-- Select --") %>

I'm not sure what I am doing wrong? I would appreciate some input :)

Thanks.