Error:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType'.
Controller:
- public ActionResult Index()
- {
- List<SelectListItem> items = new List<SelectListItem>();
- items.Add(new SelectListItem { Text = "Community", Value = "0" });
- items.Add(new SelectListItem { Text = "In-House", Value = "1", Selected = true });
- items.Add(new SelectListItem { Text = "Vacation", Value = "2" });
- items.Add(new SelectListItem { Text = "Sick", Value = "3" });
- items.Add(new SelectListItem { Text = "Absent", Value = "4" });
- items.Add(new SelectListItem { Text = "Not Scheduled", Value = "5" });
- if (ViewData["JobCodeType"] == null)
- {
- ViewData["JobCodeType"] = items;
- }
- return View();
- }
- [HttpPost]
- public ActionResult ShowDDL(FormCollection obj)
- {
- string str = obj["JobCodeType"];
- ViewBag.D = obj["JobCodeType"];
- return View("Index");
- }
Index.cshtml
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index1</title>
- </head>
- <body>
- <div>
- @using (Html.BeginForm("ShowDDL", "Home", FormMethod.Post))
- {
- @Html.DropDownList("JobCodeType")
- <input id="Submit1" type="submit" value="submit" />
- <h3>@ViewBag.D</h3>
- }
- </div>
- </body>
- </html>
Please help....................