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
.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>