Pravinkumar Birajdar

Pravinkumar Birajdar

  • 1.1k
  • 578
  • 102.8k

ASP.NET MVC Error

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

Answers (6)