Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to ASP.NET MVC I want to use a dropdownlist in mvc 4 using HTML Helper. I am stuck here. i have given the code below.

Employee controller

[HttpGet]
      public ActionResult _GetEmployeeSpeciality(string id)
      {

          DataTable dtdepartment = new DataTable();
          FPDiabeticService.BoDataTable BODT = new FPDiabeticService.BoDataTable();
          BODT = new Models.Frontdesk().FetchAllDepartmentList();
          dtdepartment = BODT.PassTable;
          List<SelectListItem> ddldepartmentlist = new List<SelectListItem>();
          ddldepartmentlist.Add(new SelectListItem { Text = "---Select a Department---", Value = "-1", Selected = true });
          if (dtdepartment.Rows.Count > 0)
          {
              for (int i = 0; i < dtdepartment.Rows.Count; i++)
              {
                  ddldepartmentlist.Add(new SelectListItem { Text = dtdepartment.Rows[i]["department"].ToString(), Value = dtdepartment.Rows[i]["departmentID"].ToString() });
              }
          }
          ViewData["ddldepartmentlist"] = ddldepartmentlist;
          return View("_GetEmployeeSpeciality");
      }


IN View

@Html.DropDownList("ddldepartmentlist")


or

@Html.DropDownList("ddldepartmentlist",ViewData["ddldepartmentlist"])


when i run this
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ddldepartmentlist'


Error Occurs can anyone helpme.


thanks
parithi
Posted
Updated 25-Aug-15 20:06pm
v5

1 solution

you have not cast the viewstate to IEnumerable<SelectListItem> that's why its giving error try this it will work

@Html.DropDownList("ddldepartmentlist",ViewData["ddldepartmentlist"] as IEnumerable<SelectListItem>)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900