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

I am getting this exception when i try to access all list,
System.InvalidOperationException: 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Dashboard.CourseLicence'.'


What I have tried:

public ActionResult CourseList()
     {
         List<eNtsaDashboardViewModel> list = new List<eNtsaDashboardViewModel>()
   {
     new eNtsaDashboardViewModel { Text = "Private(Copyrighted)", Value = "1" },
     new eNtsaDashboardViewModel { Text = "Public Domain", Value = "2" },
     new eNtsaDashboardViewModel { Text = "CC Attribution Non Directive", Value = "3" },
     new eNtsaDashboardViewModel { Text = "Creative Commons Licences", Value = "4" },

   };
         ViewBag.list = list;
         return View();
     }


@using (Html.BeginForm("CourseList", "Home", FormMethod.Post))
                                     {
                                     <div class="form-group row">
                                         <label for="Content-Licence" class="col-sm-3 col-form-label">Content Licence</label>
                                         <div class="col-sm-5">
                                             @Html.DropDownListFor(model => model.Dashboard.CourseLicence, ViewBag.list as List<SelectListItem>)
                                         </div>
                                     </div>
                                     }

// eNtsaRegCourses.
  public class RegCoursesViewModel
  {
      public eNtsaCourses Courses { get; set; }
      public eNtsaDashboardViewModel Dashboard { get; set; }
      public List<eNtsaDashboardViewModel> lsteNtsaDashboard { get; set; }
      public RegCoursesViewModel MainModel { get; set; }

  }

// This is for course-list.
 public class eNtsaDashboardViewModel
 {
     public string CourseName { get; set; }

     public string CourseLicence { get; set; }



 }
Posted
Updated 7-Sep-20 0:48am

1 solution

You can't cast your ViewBag.list as List<SelectItem> as it isn't a list of that type.

Google for examples of how to use DropDownListFor, you need to create a new SelectList item that has your list as the data and you also need to specify the property names for the data and text fields.

How to simple Html.DropDownListFor MVC.NET | The Agile Warrior[^]
 
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