Note that there are some explanatory texts on larger screens.

plurals
  1. POError when creating a dropdown - ASP.NET MVC 4 using EF Database first approach - Implementing Ninject for DI
    primarykey
    data
    text
    <p>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.</p> <p><strong>Below is my Error:</strong></p> <p>Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. </p> <p><strong>Below is my Controller</strong></p> <pre><code>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 =&gt; 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); } } </code></pre> <p><strong>I've got the below three classes (CityDTO, MyViewModel, MyModel) under Models Folder.</strong></p> <p><strong>CityDTO.cs</strong></p> <pre><code> public class CityDTO { public string CityId { get; set; } public string CityName { get; set; } } </code></pre> <p><strong>MyViewModel.cs</strong></p> <pre><code>public class MyViewModel { public string SelectedCityId { get; set; } public IEnumerable&lt;SelectListItem&gt; Cities { get; set; } } </code></pre> <p><strong>MyModel.cs</strong></p> <pre><code>public interface ICitiesRepository { IEnumerable&lt;City&gt; GetAll(); } public class CitiesRepositoryEF: ICitiesRepository { public IEnumerable&lt;City&gt; GetAll() { using (var ctx = new LocationEntities()) { return ctx.usp_GetAllCities().ToList(); } } } </code></pre> <p><strong>GlobalAsax.cs</strong></p> <pre><code> 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); } </code></pre> <p><strong>App_Start/NinjectWebCommon.cs</strong></p> <pre><code>private static void RegisterServices(IKernel kernel) { kernel.Bind&lt;Models.ICitiesRepository&gt;().To&lt;Models.CitiesRepositoryEF&gt;(); } </code></pre> <p><strong>And Below is my View:</strong></p> <pre><code>@model TestApp.Models.MyViewModel @using (Html.BeginForm()) { @Html.DropDownListFor(x=&gt;x.SelectedCityID, Model.Cities, "Select City") &lt;button type = "submit"&gt;OK&lt;/button&gt; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload