Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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**

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

  }

**MyViewModel.cs**
C#
public class MyViewModel
 {
     public string SelectedCityId { get; set; }
     public IEnumerable<SelectListItem> Cities { get; set; }
 }


**MyModel.cs**
C#
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**
C#
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**
C#
private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<models.icitiesrepository>().To<models.citiesrepositoryef>();
        }

**And Below is my View:**
HTML
@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
Updated 17-Jan-13 7:06am
v2

1 solution

Please update your viewModel like below
C#
public class MyViewModel
   {
       public string SelectedCityId { get; set; }
       public IEnumerable<city> Cities { get; set; }
   }


Hope this helps
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900