locked
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key Seq RRS feed

  • Question

  • User-523892225 posted

    After doing a search I see this question has been asked a million times but in each case the person asking the question is using ViewBag or ViewData to populate their dropdown. I am using neither and have been unable to find my situation or figure out what I am doing wrong.

    I get the above error during the initial load of the page. I don't understand why it is looking for Seq in IEnumerable<SelectListItem>.  The only Seq is coming from m.Seq which should be coming from a property in the ViewModel not the IEnumerable<SelectListItem>.  There is an Value = c.SEQ but that is different from Seq. I verified that by changing the name of Seq.

    Here is my View Model:

    namespace DHSS_Training_App.ViewModels
    {
        public class CourseEnrollmentViewModel
        {
            public IEnumerable<ILT_COURSE_TYPE> CourseTypes { get; set; }          
            public IEnumerable<ILT_COURSE_LIST> CourseList { get; set; }
            public IEnumerable<ILT_SCHEDULED_COURSES> ScheduledCourseTypes { get; set; } 
            public IEnumerable<SelectListItem> ScheduledCourseList { get; set; }
            public string Seq { get; set; }       
            public string CourseName { get; set; }
            public string MandatoryMgmtCredit { get; set; }
            public string CourseDescription { get; set; }
            public string HeaderTitle { get; set; }       
            public string CourseType { get; set; }        
            public DateTime? StartDate { get; set; }
            public DateTime? StartTime { get; set; }
            public DateTime? EndTime { get; set; }
            public string Location { get; set; }
            public string InstructorFirstName { get; set; }
            public string InstructorLastName { get; set; }
            public string Description { get; set; }
        }
    }

    The portion of my View giving me the error:

    <div class="container-fluid">
        <div class="row justify-content-center text-center">
            <div class="form-container divBackgroundColor col align-self-center mb-5"
                 style="display:@(Model.Seq != null ?"block":"none")">
                @using (Html.BeginForm("CourseEnroll", "Enroll", FormMethod.Post))
                 {
                    <div class="courseTypeDropdownStyle input-group col-">
                        @Html.DropDownListFor(m => m.Seq, Model.ScheduledCourseList, "Choose a course",
                            new { @class = "form-control form-control-style fluid-type" })
                        <div class="input-group-append col-">
                            <button class="btn btn-custom fluid-type" type="submit">Submit</button>
                        </div>
                    </div>
                 }
            </div>
        </div>
    </div>

    and my Controller:

    public ActionResult ChooseCourse(string seq)
    {            
        ViewBag.Message = "Instructor Led Training Enrollment";
    
        var coursesByType = _context.ILT_SCHEDULED_COURSES
                                .GroupBy(t => t.COURSE_TYPE)
                                .Select(t => t.FirstOrDefault())
                                .Where(t => t.COURSE_STATUS != "CANCELLED")
                                .ToList();
    
        string type = string.Empty;
    
        foreach (var item in coursesByType)
        {
            if (item.SEQ == seq)
            {
                type = item.COURSE_TYPE;
            }
        }
      
        var coursetype = _context.ILT_SCHEDULED_COURSES.Where(
             c => c.COURSE_TYPE == type.ToUpper()).ToList();
    
        CourseEnrollmentViewModel viewModel = new CourseEnrollmentViewModel
        {
            ScheduledCourseTypes = coursesByType,
            Seq = seq,
            ScheduledCourseList = coursetype.AsEnumerable().Select(c => new SelectListItem
            {
                Value = c.SEQ,
                Text = c.COURSE_NAME + " " + c.COURSE_START_DATE.ToString() + " " + c.COURSE_LOCATION + " " + c.COURSE_START_TIME.ToString()                  
            })
        };
        return View("CourseType", viewModel);    
    }

    I would appreciate any help someone could give. 

    Wednesday, September 25, 2019 1:26 PM

All replies

  • User-474980206 posted

    the error means that in:

     @Html.DropDownListFor(m => m.Seq, Model.ScheduledCourseList, "Choose a course",
                            new { @class = "form-control form-control-style fluid-type" })

    Model.ScheduledCourseList is null (list items parameter), so it so the dropdown list is looking for a property (model. view bag. etc) named Seq (text name of property) that is for type IEnumerable<SelectListItem>

    use the debugger to determine why?

    Wednesday, September 25, 2019 5:36 PM
  • User-17257777 posted

    Hi jhackney01,

    bruce is right, and there is another problem in your DropdownListFor. This is one of its overloaded

    public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes);

    This expression is used to get an IEnumerable<SelectListItem> type. For example:

    1. The object itself is this type

    2. You can get this type object from ViewBag, Model … by the expression

    I made a simple test:

    Controller:

    public ActionResult ChooseCourse()
            {
                ViewBag.Message = "Instructor Led Training Enrollment";
                
                string seq = "SEQ";
                var coursetype = new List<ILT_SCHEDULED_COURSES>
                {
                    new ILT_SCHEDULED_COURSES{SEQ = "S1", COURSE_NAME = "COURSE1"},
                    new ILT_SCHEDULED_COURSES{SEQ = "S2", COURSE_NAME = "COURSE2"},
                    new ILT_SCHEDULED_COURSES{SEQ = "S3", COURSE_NAME = "COURSE3"}
                };
                
                CourseEnrollmentViewModel viewModel = new CourseEnrollmentViewModel
                {
                    Seq = seq,
                    ScheduledCourseList = coursetype.AsEnumerable().Select(c => new SelectListItem
                    {
                        Value = c.SEQ,
                        Text = c.COURSE_NAME
                    })
                };
                return View("CourseType", viewModel);
            }
    

    View:

    <div class="container-fluid">
        <div class="row justify-content-center text-center">
            <div class="form-container divBackgroundColor col align-self-center mb-5"
                 style="display:@(Model.Seq != null ?"block":"none")">
                @using (Html.BeginForm("CourseEnroll", "Enroll", FormMethod.Post))
                {
                    <div class="courseTypeDropdownStyle input-group col-">
                        @Html.DropDownListFor(m => m.ScheduledCourseList, Model.ScheduledCourseList, "Choose a course",
                            new { @class = "form-control form-control-style fluid-type" })
                        <div class="input-group-append col-">
                            <button class="btn btn-custom fluid-type" type="submit">Submit</button>
                        </div>
                    </div>
                }
            </div>
        </div>
    </div>
    

    Test Result:

    Best Regards,

    Jiadong Meng

    Thursday, September 26, 2019 10:20 AM