Cannot implicity List into IEnumerable

111 views Asked by At

My ViewModel

public class ViewModelCT
{
    public IEnumerable<SelectListItem> Tests { get; set; }
    public IEnumerable<string> SelectedTests { get; set; }
    public Course course { get; set; }   
}

Controller

public ActionResult Details(int id)
{

    Course course = functionality.toCourse(functionality.GetCourseById(id));
    List<SelectListItem> lsli = new List<SelectListItem>();

    ViewModelCT vm = new ViewModelCT();
    foreach (SchoolService.Test sTest in functionality.GetTestByCourse(id))
    {
        SelectListItem sli = new SelectListItem()
        {
            Text = sTest.Name,
            Value = sTest.Id.ToString(),
            Selected = false
        };
        lsli.Add(sli);
       // vm.Tests.(functionality.toTest(sTest));
    }

    **vm.Tests = lsli;**
    vm.course = functionality.toCourse(functionality.GetCourseById(id));
    return View(vm);    
}

And i get error:

Error 1 Cannot implicitly convert type System.Collections.Generic.List<System.Web.Mvc.SelectListItem> to System.Collections.Generic.IEnumerable<System.Web.WebPages.Html.SelectListItem>. An explicit conversion exists (are you missing a cast?)

I don't know why it is not working.

1

There are 1 answers

0
Charles Mager On BEST ANSWER

As the error implies, System.Web.WebPages.Html.SelectListItem and System.Web.Mvc.SelectListItem are not the same thing. ViewModelCT and your controller are referencing different SelectListItem classes.

Check your using statements in each file. I suspect one has using System.Web.Mvc and the other has using System.Web.WebPages.Html.