Click here to Skip to main content
12,950,268 members (63,840 online)
Rate this:
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<tesapp.ef.ctx.usp_getallcities_result>' to 'System.Collections.Generic.IEnumerable<jquerywidgetsmvc4.models.citydto>'.

**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
            .Select(x => new SelectListItem
                Value = x.Id.ToString(),
                Text = x.Name
        return View(model);

    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.**
public class CityDTO
      public string CityId { get; set; }
      public string CityName { get; set; }


public class MyViewModel
     public string SelectedCityId { get; set; }
     public IEnumerable<SelectListItem> Cities { get; set; }

public interface ICitiesRepository
     IEnumerable<city> GetAll();

 public class CitiesRepositoryEF: ICitiesRepository
         public IEnumerable<city> GetAll()
             using (var ctx = new LocationEntities())
                 return ctx.usp_GetAllCities().ToList();

protected void Application_Start()

private static void RegisterServices(IKernel kernel)

**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>
Posted 17-Jan-13 0:45am
Updated 17-Jan-13 7:06am

1 solution

Rate this: bad
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

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,829
CHill60 3,460
Maciej Los 2,953
Jochen Arndt 1,975
ppolymorphe 1,820

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2017
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