Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Please see the code below. I am trying to create a drop down by using EF database first approach, and implementing Ninject for DI. I'm new to these concepts, and I don't know what I'm doing wrong. Any help would be greatly appreciated - thanks.
 
**Below is my Error:**
 
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'.

**Below is my Controller**
 
    public class CitiesController: Controller
    {
        private readonly ICitiesRepository repository;
        public CitiesController(ICitiesRepository repository)
        {
            this.repository = repository;
        }
    
        public ActionResult Index()
        {
            var model = new Models.MyViewModel();
            model.Cities = this
                .repository
                .GetAll()
                .ToList()
                .Select(x => new SelectListItem
                {
                    Value = x.Id.ToString(),
                    Text = x.Name
                });
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return Content("Thanks for selecting city: " + model.SelectedCityId);
        }
    }
**I've got the below three classes (CityDTO, MyViewModel, MyModel) under Models Folder.**
**CityDTO.cs**
      public class CityDTO
        {
            public string CityId { get; set; }
            public string CityName { get; set; }
    
        }
**MyViewModel.cs**
   public class MyViewModel
    {
        public string SelectedCityId { get; set; }
        public IEnumerable<SelectListItem> Cities { get; set; }
    }
 
**MyModel.cs**
   public interface ICitiesRepository
    {
        IEnumerable<city> GetAll();
    }
    
    public class CitiesRepositoryEF: ICitiesRepository
        {
            public IEnumerable<city> GetAll()
            {
                using (var ctx = new LocationEntities())
                {
                    return ctx.usp_GetAllCities().ToList();
                }
            }
        }
**GlobalAsax.cs**
     protected void Application_Start()
            {
                App_Start.NinjectWebCommon.Start();
                AreaRegistration.RegisterAllAreas();
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
**App_Start/NinjectWebCommon.cs**
    private static void RegisterServices(IKernel kernel)
            {
                kernel.Bind<models.icitiesrepository>().To<models.citiesrepositoryef>();
            }  
**And Below is my View:**
    @model TestApp.Models.MyViewModel
    
    @using (Html.BeginForm())
    { 
    @Html.DropDownListFor(x=>x.SelectedCityID, Model.Cities, "Select City")
    <button type = "submit">OK</button>
    }</models.citiesrepositoryef></models.icitiesrepository></city></city></jquerywidgetsmvc4.models.citydto></tesapp.ef.ctx.usp_getallcities_result>
Posted 17-Jan-13 0:45am
Edited 17-Jan-13 7:06am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please update your viewModel like below
public class MyViewModel
   {
       public string SelectedCityId { get; set; }
       public IEnumerable<city> Cities { get; set; }
   }
 
Hope this helps
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,935
1 OriginalGriff 6,987
2 CPallini 5,845
3 George Jonsson 4,015
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100