Rahul Mittal

Rahul Mittal

  • NA
  • 10
  • 777

List Doesn't Working in asp.net mvc4

Jan 29 2017 5:28 AM
Error Type:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Course_ID'.
 
Hi Everyone i am facing a problem as above using asp.net MVC4 technology.
 Controller:
[HttpGet]
public ActionResult Register(int id)
{
    Student_Mgt student = new Student_Mgt(); 
   IEnumerable<SelectListItem> list = new SelectList(db.Courses.ToList(), "Course_ID",    "Course_Name");
   ViewBag.SelectCourses = list;
   return View(student);
}
 
[HttpPost]
[ActionName("Register")]
public ActionResult RegisterPost(Student_Mgt Student)
{
   if (ModelState.IsValid)
   {
      db.Student_Mgt.Add(Student);
      db.SaveChanges();
      return RedirectToAction("List_Enquiries", "Main");
   }
   else
   {
      return View();
   }
 
}
 
View 
@Html.DropDownListFor(m=>m.Course_ID,ViewBag.SelectCourses as IEnumerable<SelectListItem>, "Select Course" ,new {@id="courses",@class="form-control"}) 
 

Answers (5)